For a given array count combination of pairs of (x,y) whose sum is N.
Respostas da entrevista
Sigiloso
17 de mar. de 2015
int[] list = {7,6,2,3,4,5};
int N=9;
int count=0;
for(int i=0;i
5
Sigiloso
27 de ago. de 2013
Three solutions I gave.
1. Sort the array in O(nlogn). Min and Max as two indices.
Count=0
Loop
if A[Min]+A[Max]N
Decrement (Max)
else
Increment(Count)
End
2. Using extra space and count sort if the minima and maxima of array sets are in a given range. Max absolute negative number subtracted to keep numbers in positive number space.
Loop
if Memory[N- A[i]- Min]
Increment(Count)
End
3. Create a Hashset/Hashtable. Look for N-A[i] and increment count if found.
Loop
if Get(N- A[i])
Increment (Count)
else
Push(A[i])
End
Sigiloso
26 de jul. de 2014
the complexity of ur solution is not so good you can do better
Sigiloso
10 de ago. de 2014
couldn't you just look through the array and check the sum of each combination and then check against a hashtable?! I don't understand why you have to do all the sorting stuff!