becoz you are sorting the aux[] , it seems to fine by replacing it with i < j
On 10/28/12, rahul sharma <rahul23111...@gmail.com> wrote: > I wana ask that ccan i replcae while condiion with the condition as follow > while(i<j) > > code reference :http://www.geeksforgeeks.org/archives/23338 > void findFourElements (int arr[], int n, int X) > { > int i, j; > > // Create an auxiliary array to store all pair sums > int size = (n*(n-1))/2; > struct pairSum aux[size]; > > /* Generate all possible pairs from A[] and store sums > of all possible pairs in aux[] */ > int k = 0; > for (i = 0; i < n-1; i++) > { > for (j = i+1; j < n; j++) > { > aux[k].sum = arr[i] + arr[j]; > aux[k].first = i; > aux[k].sec = j; > k++; > } > } > > // Sort the aux[] array using library function for sorting > qsort (aux, size, sizeof(aux[0]), compare); > > // Now start two index variables from two corners of array > // and move them toward each other. > i = 0; > j = size-1; > while (i < size && j >=0 ) > { > if ((aux[i].sum + aux[j].sum == X) && noCommon(aux[i], aux[j])) > { > printf ("%d, %d, %d, %d\n", arr[aux[i].first], arr[aux[i].sec], > arr[aux[j].first], arr[aux[j].sec]); > return; > } > else if (aux[i].sum + aux[j].sum < X) > i++; > else > j--; > } > } > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.