Amazon Interview Question
Software Engineer / DevelopersCountry: -
That question and the example isn't clear. It seems you want pairs of subsets. But, from your example it looks like subsets could also be from the same array ? What is the significance of the other array then? Why not just combine them into one Array?
// set default values of subSum to false ...;
for(i = 0; i < n; i++)
{
sum = sum + arr[i];
subSum[i][arr[i]] = true
}
value = 0;
for(i = 1 ; i < n; i ++)
{
for ( s = 0 ; s < sum / 2; s++)
{
subSum[i][s] = subSum[i-1][s] || (arr[i] == s) || subSum[i-1][s-arr[i]];
if(subSum[i][s])
value++;
}
}
print value;
Permute the array and get all its subsets and then group them based on sum
- nr September 13, 2011