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 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...
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...
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[] =...
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
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...
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
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...
Write a function called splice() that “splices” two integer arrays together by first allocating memory for...
Write a function called splice() that “splices” two integer arrays together by first allocating memory for a dynamic array with enough room for both integer arrays, and then copying the elements of both arrays to the new array as follows:             first, copy the elements of the first array up to a given position,             then, copy all the elements of the second array,             then, copy the remaining elements in the first array. The function should have parameters for...
13.18 Two piano strings with the same mass and the same length are to be tuned...
13.18 Two piano strings with the same mass and the same length are to be tuned to 440 Hz (A on the equal temperament scale). One string, with a tension of 300 N is already tuned to 440 Hz. The second string frequency is 435 Hz. a) Should the tension of string 2 be increased or decreased to tune the frequency to 440 Hz? b) How much must the tension of string 2 be changed to change the frequency from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT