给定两个大小分别为N 的数组A[]和B[] ,任务是找到两个数组的相同索引元素的绝对差的最小可能总和,即|A[i] – B[i]| 的总和对于所有的i,使得0≤我<由A []其中A的另一元件至多一个元件代替N []。
例子:
Input: A[] = {6, 4, 1, 9, 7, 5}, B[] = {3, 9, 7, 4, 2, 1}, N = 6
Output: 22
Explanation: Replace A[2] with A[4]. The array A[] modifies to [6, 4, 7, 9, 7, 5]. This yields an absolute sum difference of |6 – 3| + |4 – 9| + |7 – 7| + |9 – 4| + |7 – 2| + |5 – 1| = 22, which is maximum possible.
Input: A[] = {2, 5, 8}, B[] = {7, 6, 1}, N = 3
Output: 7
方法:解决问题的想法是使用二分搜索计算每个B[i] (0 ≤ i < N)中A[] 中与B[i]最接近的元素,并选出最佳选择。
请按照以下步骤解决问题。
- 初始化大小为N 的两个数组diff[]和BestDiff[] 。
- 将变量sum初始化为0 。
- 从0到N – 1遍历,对于每个i存储diff[i]= abs(A[i] – B[i])并将diff[i]添加到sum 。
- 按升序对数组A[]进行排序。
- 迭代索引0到N – 1并且对于每个i ,使用二进制搜索,在A[]中找到最接近B[i]的元素,比如说X并将其存储在BestDiff[] 中作为BestDiff[i] = abs (B[i] – X)
- 初始化一个变量BestPick 。
- 迭代索引0到N – 1并将BestPick更新为BestPick = max(BestPick, diff[i]-BestDiff[i])
- 现在, Sum – BestPick给出了答案。
下面是上述方法的一个实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return minimum sum of absolute
// difference of same-indexed elements of two arrays
int minAbsoluteSumDiff(vector A,
vector B, int N)
{
// Stores initial sum
int sum = 0;
// Stores the differences between
// same-indexed elements of A[] and B[]
int diff[N];
for (int i = 0; i < N; i++) {
// Update absolute difference
diff[i] = abs(A[i] - B[i]);
// Update sum of differences
sum += diff[i];
}
// Sort array A[] in ascending order
sort(A.begin(), A.end());
// Stores best possible
// difference for each i
int bestDiff[N];
for (int i = 0; i < N; i++) {
// Find the index in A[]
// which >= B[i].
int j = lower_bound(A.begin(), A.end(), B[i])
- A.begin();
// Store minimum of abs(A[j] - B[i])
// and abs(A[j - 1] - B[i])
if (j != 0 && j != N)
bestDiff[i] = min(abs(A[j] - B[i]),
abs(A[j - 1] - B[i]));
// If A[j] can be replaced
else if (j == 0)
bestDiff[i] = abs(A[j] - B[i]);
// If A[j - 1] can be replaced
else if (j == N)
bestDiff[i] = abs(A[j - 1] - B[i]);
}
// Find best possible replacement
int bestPick = 0;
for (int i = 0; i < N; i++) {
bestPick = max(bestPick,
diff[i] - bestDiff[i]);
}
// Return the result
return sum - bestPick;
}
// Driver code
int main()
{
// Input
vector A = { 2, 5, 8 };
vector B = { 7, 6, 1 };
int N = 3;
cout << minAbsoluteSumDiff(A, B, N) << endl;
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Recursive implementation of
// lower_bound
static int lower_bound(int arr[], int low, int high,
int X)
{
// Base Case
if (low > high) {
return low;
}
// Find the middle index
int mid = low + (high - low) / 2;
// If arr[mid] is greater than
// or equal to X then search
// in left subarray
if (arr[mid] >= X) {
return lower_bound(arr, low, mid - 1, X);
}
// If arr[mid] is less than X
// then search in right subarray
return lower_bound(arr, mid + 1, high, X);
}
// Function to return minimum sum of absolute
// difference of same-indexed elements of two arrays
static int minAbsoluteSumDiff(int A[], int B[], int N)
{
// Stores initial sum
int sum = 0;
// Stores the differences between
// same-indexed elements of A[] and B[]
int diff[] = new int[N];
for (int i = 0; i < N; i++) {
// Update absolute difference
diff[i] = Math.abs(A[i] - B[i]);
// Update sum of differences
sum += diff[i];
}
// Sort array A[] in ascending order
Arrays.sort(A);
// Stores best possible
// difference for each i
int bestDiff[] = new int[N];
for (int i = 0; i < N; i++) {
// Find the index in A[]
// which >= B[i].
int j = lower_bound(A, 0, N - 1, B[i]);
// Store minimum of abs(A[j] - B[i])
// and abs(A[j - 1] - B[i])
if (j != 0 && j != N)
bestDiff[i]
= Math.min(Math.abs(A[j] - B[i]),
Math.abs(A[j - 1] - B[i]));
// If A[j] can be replaced
else if (j == 0)
bestDiff[i] = Math.abs(A[j] - B[i]);
// If A[j - 1] can be replaced
else if (j == N)
bestDiff[i] = Math.abs(A[j - 1] - B[i]);
}
// Find best possible replacement
int bestPick = 0;
for (int i = 0; i < N; i++) {
bestPick
= Math.max(bestPick, diff[i] - bestDiff[i]);
}
// Return the result
return sum - bestPick;
}
// Driver code
public static void main(String[] args)
{
// Input
int A[] = { 2, 5, 8 };
int B[] = { 7, 6, 1 };
int N = 3;
System.out.println(minAbsoluteSumDiff(A, B, N));
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program for the above approach
from bisect import bisect_left,bisect_right
# Function to return minimum sum of absolute
# difference of same-indexed elements of two arrays
def minAbsoluteSumDiff(A, B, N):
# Stores initial sum
sum = 0
# Stores the differences between
# same-indexed elements of A[] and B[]
diff = [0] * N
for i in range(N):
# Update absolute difference
diff[i] = abs(A[i] - B[i])
# Update sum of differences
sum += diff[i]
# Sort array A[] in ascending order
A.sort()
# Stores best possible
# difference for each i
bestDiff = [0] * N
for i in range(N):
# Find the index in A[]
# which >= B[i].
j = bisect_left(A, B[i])
# Store minimum of abs(A[j] - B[i])
# and abs(A[j - 1] - B[i])
if (j != 0 and j != N):
bestDiff[i] = min(abs(A[j] - B[i]),
abs(A[j - 1] - B[i]))
# If A[j] can be replaced
elif (j == 0):
bestDiff[i] = abs(A[j] - B[i])
# If A[j - 1] can be replaced
elif (j == N):
bestDiff[i] = abs(A[j - 1] - B[i])
# Find best possible replacement
bestPick = 0
for i in range(N):
bestPick = max(bestPick,
diff[i] - bestDiff[i])
# Return the result
return sum - bestPick
# Driver code
if __name__ == "__main__":
# Input
A = [ 2, 5, 8 ]
B = [ 7, 6, 1 ]
N = 3
print(minAbsoluteSumDiff(A, B, N))
# This code is contributed by ukasp
C#
// C# program for the above approach
using System;
class GFG {
// Recursive implementation of
// lower_bound
static int lower_bound(int []arr, int low, int high, int X)
{
// Base Case
if (low > high) {
return low;
}
// Find the middle index
int mid = low + (high - low) / 2;
// If arr[mid] is greater than
// or equal to X then search
// in left subarray
if (arr[mid] >= X) {
return lower_bound(arr, low, mid - 1, X);
}
// If arr[mid] is less than X
// then search in right subarray
return lower_bound(arr, mid + 1, high, X);
}
// Function to return minimum sum of absolute
// difference of same-indexed elements of two arrays
static int minAbsoluteSumDiff(int []A, int []B, int N)
{
// Stores initial sum
int sum = 0;
// Stores the differences between
// same-indexed elements of A[] and B[]
int []diff = new int[N];
for (int i = 0; i < N; i++) {
// Update absolute difference
diff[i] = Math.Abs(A[i] - B[i]);
// Update sum of differences
sum += diff[i];
}
// Sort array A[] in ascending order
Array.Sort(A);
// Stores best possible
// difference for each i
int []bestDiff = new int[N];
for (int i = 0; i < N; i++) {
// Find the index in A[]
// which >= B[i].
int j = lower_bound(A, 0, N - 1, B[i]);
// Store minimum of abs(A[j] - B[i])
// and abs(A[j - 1] - B[i])
if (j != 0 && j != N)
bestDiff[i]
= Math.Min(Math.Abs(A[j] - B[i]),
Math.Abs(A[j - 1] - B[i]));
// If A[j] can be replaced
else if (j == 0)
bestDiff[i] = Math.Abs(A[j] - B[i]);
// If A[j - 1] can be replaced
else if (j == N)
bestDiff[i] = Math.Abs(A[j - 1] - B[i]);
}
// Find best possible replacement
int bestPick = 0;
for (int i = 0; i < N; i++) {
bestPick
= Math.Max(bestPick, diff[i] - bestDiff[i]);
}
// Return the result
return sum - bestPick;
}
// Driver code
public static void Main()
{
// Input
int []A = { 2, 5, 8 };
int []B = { 7, 6, 1 };
int N = 3;
Console.Write(minAbsoluteSumDiff(A, B, N));
}
}
// This code is contributed by ipg2016107.
Javascript
输出:
7
时间复杂度: O(NLogN)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live