First I will explain little bit about hashSet. look up in hashSet is very
quick, hashSet always maintains only unique element.
algo:
sum = x;
for i = 0 to array[arrayLength - 1]
if array[i] in hashSet :
return (array[i], sum - array[i])
else:
hashSet.add (sum-a[i])
For eg: lets take array = {7,2,3,1,-5,8,15};
hashSet=empty;
for a[0] i.e., 7 -> else condn -> put 3 (sum-a[i]) in hashSet; (so that
next time if a[i] present in hashSet you can say found a pair which gives
sum =x; ) hashSet = <3>
for a[1] -> else condn -> put 2 in hashSet ; hashSet = <3,2>
for a[2] -> if condn -> return <3,7> ;
etc etc....
If you are not clear still, take an array & start executing the code urself
(update all variables accordingly).
At the end of it,you wil be able to understand the logic
On Tue, Jun 22, 2010 at 1:43 PM, sharad kumar <[email protected]>wrote:
> i knoe c++ well so i can understand java code bt
> i m not able to understand this code so plzzzz explain it or give algo
> thnx in advance
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
cheers
Jeeva
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.