@samm:you are checking for each sum between min sum and max sum & in each
loop we are skipping several triplets(sum)........ which hai intermediate
sum..........this is also a o(n^3) brute-force almost.......i have written
a code jst c it .........can u think in this direction so dat we wl b able
to cum on a right code.........this is efficient but it is skipping some
cases..........jst think on sum part so that we can get sum widout using
loop.........tnx
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
main()
{
int n;
int a[40001],b[60001];
memset(b,0,sizeof(b));
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int max=-60001;
for(int i=0;i<n-2;i++)
{
int l=i+1;
int h=n-1;
int k;
while(l<h)
{
k=a[l]+a[h]+a[i];
b[k]++;
if(max<k)
max=k;
l++;
}
l=i+1;
h=n-2;
while(l<h)
{
k=a[l]+a[h]+a[i];
b[k]++;
if(max<k)
max=k;
h--;
}
}
for(int i=0;i<=max;i++)
if(b[i])
cout<<i<<" : "<<b[i]<<"\n";
}
--
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.