Re: [algogeeks] Algorithm to find two numbers in an array that sum up to a given number

2011-08-31 Thread jai gupta
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.



Re: [algogeeks] Algorithm to find two numbers in an array that sum up to a given number

2011-08-31 Thread SANDEEP CHUGH
can be done in O(n) with hashing

On Wed, Aug 31, 2011 at 11:09 AM, Ankit Minglani
wrote:

> one method can be :
>
> Let T = the sum
>
> for(i=0;i {
>temp=T- x[i];
>// perform a binary search on array[ i+1. n-1] to find temp it if
> does then just output.
> }
>
>
>
>
>
>
>
>
>
>
>
>
> On Wed, Aug 31, 2011 at 11:04 AM, bharatkumar bagana <
> bagana.bharatku...@gmail.com> wrote:
>
>> Its brute force method.O(n^2) algo...
>> for(int i=0;i>   for(int j=i+1;j>   {
>>  if(x==A[i]+A[j])
>>   { print A[i],A[j]; break;}
>>
>>   }
>>
>> On Wed, Aug 31, 2011 at 1:15 AM, Reynald  wrote:
>>
>>> For example, in array, say,
>>> <8, 9, 2, 4, 6, 2, 3>
>>>
>>> Input 5, output 2 and 3.
>>>
>>> --
>>> 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.
>>>
>>>
>>
>>
>> --
>>
>> **Please do not print this e-mail until urgent requirement. Go Green!!
>> Save Papers <=> Save Trees
>> *BharatKumar Bagana*
>> **http://www.google.com/profiles/bagana.bharatkumar
>> *
>> Mobile +91 8056127652*
>> 
>>
>>
>>  --
>> 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.
>>
>
>
>
> --
> The more you sweat in the field, the less you bleed in war."
>
> Ankit Minglani
> NITK Surathkal
>
>  --
> 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.



Re: [algogeeks] Algorithm to find two numbers in an array that sum up to a given number

2011-08-30 Thread Ankit Minglani
one method can be :

Let T = the sum

for(i=0;i wrote:

> Its brute force method.O(n^2) algo...
> for(int i=0;i   for(int j=i+1;j   {
>  if(x==A[i]+A[j])
>   { print A[i],A[j]; break;}
>
>   }
>
> On Wed, Aug 31, 2011 at 1:15 AM, Reynald  wrote:
>
>> For example, in array, say,
>> <8, 9, 2, 4, 6, 2, 3>
>>
>> Input 5, output 2 and 3.
>>
>> --
>> 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.
>>
>>
>
>
> --
>
> **Please do not print this e-mail until urgent requirement. Go Green!!
> Save Papers <=> Save Trees
> *BharatKumar Bagana*
> **http://www.google.com/profiles/bagana.bharatkumar
> *
> Mobile +91 8056127652*
> 
>
>
>  --
> 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.
>



-- 
The more you sweat in the field, the less you bleed in war."

Ankit Minglani
NITK Surathkal

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



Re: [algogeeks] Algorithm to find two numbers in an array that sum up to a given number

2011-08-30 Thread saurabh singh
Discussed so many times on the group
search in the group archive..No point repeating things again and again

On Wed, Aug 31, 2011 at 11:04 AM, bharatkumar bagana <
bagana.bharatku...@gmail.com> wrote:

> Its brute force method.O(n^2) algo...
> for(int i=0;i   for(int j=i+1;j   {
>  if(x==A[i]+A[j])
>   { print A[i],A[j]; break;}
>
>   }
>
> On Wed, Aug 31, 2011 at 1:15 AM, Reynald  wrote:
>
>> For example, in array, say,
>> <8, 9, 2, 4, 6, 2, 3>
>>
>> Input 5, output 2 and 3.
>>
>> --
>> 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.
>>
>>
>
>
> --
>
> **Please do not print this e-mail until urgent requirement. Go Green!!
> Save Papers <=> Save Trees
> *BharatKumar Bagana*
> **http://www.google.com/profiles/bagana.bharatkumar
> *
> Mobile +91 8056127652*
> 
>
>
>  --
> 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.
>



-- 
Saurabh Singh
B.Tech (Computer Science)
MNNIT ALLAHABAD

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



Re: [algogeeks] Algorithm to find two numbers in an array that sum up to a given number

2011-08-30 Thread bharatkumar bagana
Its brute force method.O(n^2) algo...
for(int i=0;i wrote:

> For example, in array, say,
> <8, 9, 2, 4, 6, 2, 3>
>
> Input 5, output 2 and 3.
>
> --
> 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.
>
>


-- 

**Please do not print this e-mail until urgent requirement. Go Green!!
Save Papers <=> Save Trees
*BharatKumar Bagana*
**http://www.google.com/profiles/bagana.bharatkumar
*
Mobile +91 8056127652*


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



[algogeeks] Algorithm to find two numbers in an array that sum up to a given number

2011-08-30 Thread Reynald
For example, in array, say,
<8, 9, 2, 4, 6, 2, 3>

Input 5, output 2 and 3.

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