(for array sorted in ascending order) take 2 indexes i and j pointing to 1st and last element of the array respectively... now if(arr[i]+arr[j] == x) print(arr[i]); print(arr[j]; else if(arr[i]+arr[j]>x) j--; else i++;
I think this should work...(i've not checked) correct me if i m wrong On 6/27/11, ankit sambyal <ankitsamb...@gmail.com> wrote: > @Bharath : Cud u plz explain how r u searching the elements in O(n) time? > Because if we use binary search, it will have O(n*log n ) worst case > time complexity. One way in which I think it cud be made O(n) is that > we can use a hash table, with a good hash function apart frm the > array. And then for each element 'm' in the array, we cud search if > there is an element (sum - m) in O(1) time by using hash table. Still > we can't assure O(n) time complexity. Because coming up with a good > hash function is not easy. Again, hash table takes more space.... > > > > > On Mon, Jun 27, 2011 at 1:40 AM, Nishant Mittal > <mittal.nishan...@gmail.com> wrote: >> do inorder traversal of tree and store values in an array. >> Now find pairs by applying binary search on array.. >> >> On 6/27/11, manish kapur <manishkapur.n...@gmail.com> wrote: >>> >>> >>> -- >>> 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. >> >> > > -- > 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.