Re: [algogeeks] Re: Given an array A[] and a integer num. Find four no.s in the array whose sum is equal to given num.

2011-08-28 Thread Mohit Goel
This code may also work .give any counter examples... #includeiostream using namespace std; void find_sum(int num,int k,int j,int b); void display(int i,int j); #define MAX 8 int res[4]; //array to store 4 numbers.. int arr[MAX]

Re: [algogeeks] Re: Given an array A[] and a integer num. Find four no.s in the array whose sum is equal to given num.

2011-08-28 Thread muthu raj
@Dave: Explain your O(n3) solution please *Muthuraj R IV th Year , ISE PESIT , Bangalore* On Sat, Aug 27, 2011 at 11:10 PM, Mohit Goel mohitgoel291...@gmail.comwrote: This code may also work .give any counter examples... #includeiostream using namespace std; void find_sum(int

[algogeeks] Re: Given an array A[] and a integer num. Find four no.s in the array whose sum is equal to given num.

2011-08-28 Thread Dave
@Muthu: Sort the data. Then start two indices: i1 = 0 to n-3, i2 = i1+1 to n-2. Inside these two loops, set i3 = i2+1 and i4 = n-1. Then loop while i3 i4, incrementing i3 if a[i1]+a[i2]+a[i3]+a[i4] num, decrementing i4 if a[i1]+a[i2]+a[i3]+a[i4] num, or outputting the result if equality. Dave

[algogeeks] Re: Given an array A[] and a integer num. Find four no.s in the array whose sum is equal to given num.

2011-08-27 Thread Dave
@Dheeraj: This is a great solution. My first thought has complexity O(n^3) and uses O(1) extra space. For your algorithm, sorting an array of size n^2 would be O(n^2 log n), so your algorithm has complexity O(n^2 log n) and uses extra space of size O(n^2). We can see the tradeoff of space for