Narayan
BAN USERHi below is my code let me know if anything is wrong
int a[]={1,2,5,1,6,4,9,7,0,3};
int s=3;//size of subset
int n=a.length;
int temp=0;
int expected_sum=9;
int j,i,k;
for( i=0;i<n;i++)
{temp=0;
for( j=i+1;j<(i+(s-1))&&j<n;j++)
{
temp+=a[i]+a[j];
}
for( k=j;k<n;k++)
{
expected_sum=temp+a[k];
if (expected_sum==9)
{
System.out.println("Elements are at the index"+i+--j+k);
}
}
}
Gud answer ming, but i would want to include one more condition in the code, get the maximum and minimum value that can be present in an array i.e max=a[i]+n and min=a[i]-n and expected num should be present within this limit otherwise we can say that num does not exist this improves the efficiency of the code.let me know if it's wrong.
- Narayan June 14, 2012
- Narayan April 17, 2015