Heey dude !!

He has given the link tht show the way how to find the sum from a
distinct , you need to manipulate the algorithm .

Below is the modified code .

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int N[40001]={0};
int compare(const void *a,const void *b)
{
return( (*(int*)a)-(*(int*)b));
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&N[i]);
qsort(N,n,sizeof(int),compare);
int a,b,c,k,l;
int max=N[n-1]+N[n-2]+N[n-3],min=N[0]+N[1]+N[2];
//printf("%d %d\n",min,max);
for(int s=min;s<=max;s++)
{
int x=0;
for(int i=0;i<=n-3;i++)
{
a=N[i];
k=i+1,l=n-1;
while(k<l)
{
b=N[k],c=N[l];
if((a+b+c)==s) {x++;k=k+1,l=l-1;}
else if((a+b+c)>s) l=l-1;
else k=k+1;
}
}
if(x) printf("%d : %d\n",s,x);
}
return 0;
}

See if it works .. Let me know ur concern ..

I guess we can still reduce the complexity ..

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

Reply via email to