With hashing..
make a hash table of elements from 0 to sum/2
if an element k is in sum/2 to sum then check if sum-k is in the hashtable.
take care of the case when sum is even and sum/2 occurs only once.
TC: O(n) Space complexity: O(sum)

Method2: Sort the elements.
Now maintain to pointers one at each end of the list and check their sum.
Move the pointers towards each other depending on if their sum is greater
than the required sum or less than it.
TC: O(nlogn) SC: O(1)

-- 
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.

Reply via email to