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...
. 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 ?[?] + ?[?]...
Given two arrays: A[0..m-1] and B[0..n-1]. Find whether B[] is a subset of A[] or not....
Given two arrays: A[0..m-1] and B[0..n-1]. Find whether B[] is a subset of A[] or not. Both the arrays are not in sorted order. It may be assumed that elements in both array are distinct. Design an O(n) time algorithm that solves this problem. Provide pseudocode Ex: Input: A[] = {11, 1, 13, 21, 3, 7}, B[] = {11, 3, 7, 1} Output: B[] is a subset of A[] Input: A[] = {1, 2, 3, 4, 5, 6}, B[] =...
write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
In arduino: 1.Given two Arrays A= {2,4,7,8,3) and B={11,3,2,8,13). Write a program that find the numbers...
In arduino: 1.Given two Arrays A= {2,4,7,8,3) and B={11,3,2,8,13). Write a program that find the numbers into A but not in B. For the example C=A-B= {4,7} Print the values (Use for loops) 2.Create a vector of five integers each in the range from -10 to 100 (Prompt the user for the five integer x). Perform each of the following using loops: a) Find the maximum and minimum value b)Find the number of negatives numbers Print all results
Let's say that you are looking to invest in two stocks A and B. Stock A...
Let's say that you are looking to invest in two stocks A and B. Stock A has a beta of 1.19 and based on your best estimates is expected to have a return of 13%. Stock B has a beta of 1.61 and is expected to earn 7%. If the risk-free rate is currently 2% and the expected return on the market is 12%, which stock(s) should you invest in, if any? Work by hand no financial calculator
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The class should have one static methods: public static boolean compare(int[] arrayOne, int[] arrayTwo) – Which compares the two arrays for equality. The two arrays are considered equal if they are the same size, and contain the same elements in the same order. If they are equal, the method should return true. Otherwise, the method should return false. NOTE: You are not allowed to use...
1. Consider two blood vessels that have the same length and resistance. Given these pressure differentials...
1. Consider two blood vessels that have the same length and resistance. Given these pressure differentials along their lengths, which one will have the greatest flow? Why? Vessel 1: P1 = 100 mmHg, P2 = 25 mmHg Vessel 2: P1 = 500 mmHg, P2 = 400 mmHg 2. A healthy student requires a cardiac output of 4000mL per minute to support resting activity. The interval between beats is 0.8 secs. In moderate exercise, such as walking, the student’s heart rate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT