Question

In: Computer Science

You are given two integer arrays a and b of the same length. Let's define the...

You are given two integer arrays a and b of the same length. Let's define the difference between a and b as the sum of absolute differences of corresponding elements:

difference = |a[0] - b[0]| + |a[1] - b[1]| + ... + |a[a.length - 1] - b[b.length - 1]|

You can replace one element of a with any other element of a. Your task is to return the minimum possible difference between a and b that can be achieved by performing at most one such replacement on a. You can also choose to leave the array intact.

Example:

For a = [1, 3, 5] and b = [5, 3, 1], the output should be minDiffOfArrays(a, b) = 4.
If we leave the array a intact, the difference is |1 - 5| + |3 - 3| + |5 - 1| = 8;
If we replace a[0] with a[1], we get a = [3, 3, 5] and the difference is |3 - 5| + |3 - 3| + |5 - 1| = 6;
If we replace a[0] with a[2], we get a = [5, 3, 5] and the difference is |5 - 5| + |3 - 3| + |5 - 1| = 4;
If we replace a[1] with a[0], we get a = [1, 1, 5] and the difference is |1 - 5| + |1 - 3| + |5 - 1| = 10;
If we replace a[1] with a[2], we get a = [1, 5, 5] and the difference is |1 - 5| + |5 - 3| + |5 - 1| = 10;
If we replace a[2] with a[0], we get a = [1, 3, 1] and the difference is |1 - 5| + |3 - 3| + |1 - 1| = 4;
If we replace a[2] with a[1], we get a = [1, 3, 3] and the difference is |1 - 5| + |3 - 3| + |3 - 1| = 6;
The final answer is 4, since it's the minimum possible difference.

Solutions

Expert Solution

In this question task is to return the min possible value of sum of absolute of difference of a[i] and b[i]. the condition imposed is that at max one value of a[i] can be replaced with other a array's value.

Lets suppose absval is sum of absolute of difference of a[i] and b[i] at the beginning.

So to solve this optimally choose two index of array a such that replacing (index i )value with (index j) value will give us max possible decrement in absval.

Lets call that max possible achievable decrement as diff.

Now for each index(i) of array a look for value of array a index (j) such that replacing a[i] with a[j] give max possible decrement in absval for that index. to do this store value of array a in c and d array.

sort them now use lower_bound function in each of c and d to find closest element to b[i] in array a which will give us most optimal solution.

use this approach for each index and will find maxpossible decrement that can be achieved in initial absval.

final answeer will be (absval-diff).

Code for above logic is given below

*lower_bound (a,x) return first positon in array a which has value >=x(nearest greater value to x)

*lower_bound (a,x) return first positon in array a which has value <=x(nearest smaller value to x)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
int a[n],b[n],c[n],d[n];
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
cin>>b[i];
for(int i=0;i<n;i++)
{
c[i]=a[i];
d[i]=a[i];
}
sort(c,c+n);
sort(d,d+n,greater<>());
int absval=0;
for(int i=0;i<n;i++)
absval+=abs(a[i]-b[i]);
int maxdiff=0;
for(int i=0;i<n;i++)
{
  int index1 = lower_bound(c,c+n,b[i]) -c;//closest value to b[i] in a in right side
  int index2 = lower_bound(d,d+n,b[i]) -d;//closest value to b[i] in a in left side
  int diff1=0,diff2=0;
  if(index1<n)
  diff1 = abs(a[i]-b[i]) - abs(c[index1]-b[i]); 
  if(index2<n)
  diff2 = abs(a[i]-b[i]) - abs(c[index2]-b[i]);
  maxdiff = max(0,max(diff1,diff2));//max possible decrement 
}
cout<<absval-maxdiff;
}

Output and program snapshots.


Related Solutions

Project: Given a string s and an integer array indices of the same length. The string...
Project: Given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the i th position moves to indices[i] in the shuffled string. Return the shuffled string. Example: Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. You need to do: Create a class called ShuffledStringApp. Keeping class StringOperation and IO in util package. In this project, you will...
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
in C++ Given two integer arrays sorted in the ascending order, code the function SortArrays to...
in C++ Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include <iostream> using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67};...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM] =...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include <iostream> using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM]...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include <iostream> using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM]...
. As input you are given two arrays: an array of numbers ? and an array...
. As input you are given two arrays: an array of numbers ? and an array ? of queries where each query is a target number. The array ? is unsorted and may contain duplicates. Your goal is, for each query ? in the array ?, count the number of pairs in the array ? that sums up to ?; that is, the number of distinct pairs of indices [?, ?], with ? < ?, such that ?[?] + ?[?]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT