@SVIX: Yes, simple hasing will work. After processing the first 4
numbers, the hash table contains 1, 2, 3, and 6. When you process the
second 6, you search for 12 - 6 = 6 and find it. The two numbers in
the array that sum to 12 are 6 and 6. What's the problem with that?

Dave

On May 20, 6:29 pm, SVIX <saivivekh.swaminat...@gmail.com> wrote:
> @Dave... one more thought...
>
> lets say the array is {1,2,3,6,6} (duplicate numbers).... and u wanna
> see if 2 numbers add up to 12.... simple hashing wont work...
>
> On May 20, 8:01 am, Dave <dave_and_da...@juno.com> wrote:
>
>
>
> > @Aakash: And tell me how map works. Is making an entry O(1) regardless
> > of the value of the entry? For example, is it O(n) to map the sequence
> > 1, 4, 9, 16, 25, ..., n^2?
>
> > Dave
>
> > On May 20, 9:39 am, Aakash Johari <aakashj....@gmail.com> wrote:
>
> > > @Dave: I got you. I will have to check before pushing the element in map.
>
> > > On Fri, May 20, 2011 at 7:30 AM, Dave <dave_and_da...@juno.com> wrote:
> > > > @Aakash: Yeah, but try the same array with sum = 6 and see what
> > > > happens.
>
> > > > Dave
>
> > > > On May 20, 9:04 am, Aakash Johari <aakashj....@gmail.com> wrote:
> > > > > int main()
> > > > > {
> > > > >         int a[10] = {5, 3, 10, 9, 8, 23, 11, 4, 12, 6};
> > > > >         int i;
> > > > >         int sum;
> > > > >         int flag = 0;
>
> > > > >         map<int, int> m;
>
> > > > >         for ( i = 0; i < 10; i++ ) {
> > > > >                 m[a[i]] = 1;
> > > > >         }
>
> > > > >         sum = 13;
>
> > > > >         for ( i = 0; i < 10; i++ ) {
> > > > >                 if ( m[sum - a[i]] == 1 ) {
> > > > >                         flag = 1;
> > > > >                         break;
> > > > >                 }
> > > > >         }
>
> > > > >         if ( flag == 1 )
> > > > >                 cout << a[i] << " " << sum - a[i] << endl;
>
> > > > >         return 0;
>
> > > > > }
> > > > > On Fri, May 20, 2011 at 7:01 AM, hari <rajakin...@gmail.com> wrote:
> > > > > > We can sort using STL sort function in main() before function call 
> > > > > > of
> > > > > > arraysum().
>
> > > > > > On May 20, 6:49 am, Gunjan Sharma <gunjan.khan...@gmail.com> wrote:
> > > > > > > First of all there is an infinite loop in this code....
> > > > > > > Secondly it works only for sorted array.
>
> > > > > > > On Fri, May 20, 2011 at 7:16 PM, hari <rajakin...@gmail.com> 
> > > > > > > wrote:
> > > > > > > > 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.
>
> > > > > > > --
> > > > > > > Regards
> > > > > > > Gunjan Sharma
> > > > > > > B.Tech IV year CSE
>
> > > > > > > Contact No- +91 9997767077
>
> > > > > > --
> > > > > > 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.
>
> > > > > --
> > > > > -Aakash Johari
> > > > > (IIIT Allahabad)- Hide quoted text -
>
> > > > > - Show quoted text -
>
> > > > --
> > > > 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.
>
> > > --
> > > -Aakash Johari
> > > (IIIT Allahabad)- Hide quoted text -
>
> > > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

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