Find max common sum of subsets of 2 arrays
Given 2 Arrays of Intergers (unsorted, may contains duplicate elements), e.g.:
int[] left = {1, 5, 3};
int[] right = {2, 2};
We can get sums of subset of left array by picking or not picking up each element (2^n combinations), so, all the possbile sums could be (remove the duplicate sums):
{0, 1, 3, 4, 5, 6, 8, 9}
Same thing to the right array, sums of subset of right array are:
{0, 2, 4}
Then, the max common sum of subsets of these 2 arrays is 4, because 4 = left[0] + left[2] = rihgt[0] + right[1] and it's the max.
Question: how to get max common sum and indexes to construct this sum from 2 arrays? (if there are multipe combinations could get the same max sum in one array, just need to return one combination) Any better way to get the max common sum without caculating out all the possbile sums of subset?
from Recent Questions - Stack Overflow https://ift.tt/3hLVmUr
https://ift.tt/eA8V8J
Comments
Post a Comment