[algogeeks] Re: Given an array, find out if there exists a pair of nos. that adds up to a sum 'x'

2010-09-14 Thread soundar
#include iostream #include cstring #include conio.h using namespace std; int main() { int n,a[30],temp[30],x; cin n; memset(temp,0,sizeof(temp)); for(int i=0;in;i++) cin a[i]; cin x; temp[x-a[0]]=1; for(int i=1;in;i++) { if(temp[a[i]]==1)

Re: [algogeeks] Re: Given an array, find out if there exists a pair of nos. that adds up to a sum 'x'

2010-09-14 Thread TurksHead Education
A very nice explanation at http://www.rawkam.com/?p=345 On Tue, Sep 14, 2010 at 3:16 PM, soundar soundha...@gmail.com wrote: #include iostream #include cstring #include conio.h using namespace std; int main() { int n,a[30],temp[30],x; cin n; memset(temp,0,sizeof(temp));

Re: [algogeeks] Re: Given an array, find out if there exists a pair of nos. that adds up to a sum 'x'

2010-09-13 Thread Raj N
use mapint,bool S when you encounter a particular number check S[x-num] is true, if so return that pair. else S[num]=true On Sat, Sep 11, 2010 at 1:09 PM, topojoy biswas topojoy.bis...@gmail.comwrote: put all the numbers in a hash table( which demands O(n) space) and then pick each number in

[algogeeks] Re: Given an array, find out if there exists a pair of nos. that adds up to a sum 'x'

2010-09-10 Thread jagadish
Hi, O(n) solution is possible if the array is presorted! Else i think it is NOT. 1.Have two ptrs (one from first and other to track the array from last) 2.s=a[left]+a[right] 3.if(ssum) left++; else if(ssum) right--; else print sum found On Sep 10, 10:19 pm, bittu shashank7andr...@gmail.com