In while loop have i,j which points first and last index of array. In while loop, Check the sum of a[i],a[j], If sum<k,increment i or else decrement j. Run the while loop till i<j..
CODE: int arraysum(int a[], int k, int i, int j) while(i<j) { int p=0; int b[10]; //to store index of selected nos sum=a[i]+a[j]; if (sum==k) { b[p++]=i;b[p++]=j; } elseif(sum<k) i++; else(sum>k) j++; return b; } On May 20, 4:38 am, amit <amitthecoo...@gmail.com> wrote: > given an array of integers, and an integer k, find out two elements > from the array whose sum is k in O(n) time. if no such element exists > output none. -- 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.