Re: [algogeeks] Sum of Four Numbers in an Array (Amazon)

2012-01-01 Thread atul anand
if you want to find all combination of number which will sum to K then change block if(a+b+c+d == num) and remove flag and break to :- if(a+b+c+d == num) { printf(\nNumber found (%d + %d + %d + %d ) = %d,a,b,c,d,num); k++; l--; m--; } -- You received this message because you are subscribed

Re: [algogeeks] Sum of Four Numbers in an Array (Amazon)

2012-01-01 Thread atul anand
sort(arr); min=arr[0]+arr[1]+arr[2]+arr[3]; max=arr[n-1]+arr[n-2]+arr[n-3]+arr[n-4]; for(i=0;in-3;i++) { a=arr[i]; k=i+1; l=n-1; m=n-2; while(kl) { b=arr[k]; c=arr[l]; d=arr[m]; if(a+b+c+d == num) { printf(\nNumber found (%d + %d + %d + %d ) =

Re: [algogeeks] Sum of Four Numbers in an Array (Amazon)

2012-01-01 Thread Arun Vishwanathan
does this make sense? 1)sort array O(nlogn) 2)keep 4 pointer to last 4 elements of array. At each point in the algorithm we need to ensure than ijkl where i j k and l are positions of the 4 pointers. 3)if(sum of those 4 elements k) there exists no such combination else do binary search with all