+2 votes
40 views

For example if input integer array is {2, 6, 3, 9, 11} and given sum is 9, output should be {6,3}.

asked Feb 6 in Computer Science - IT by ANURAJ KUMAR DWIVEDI | 40 views

1 Answer

+1 vote

It is simple, we have to use for loop inside a loop;

here its a simple solution for this.

#include<bits/c++config.h>
int n,i,j,a[n],b;
cin>>n; //its the size of array;
{
for(i=0;i<n;i++)
{cin>>a[n];}
} cin>>b; //element to be searched;

for(i=0;i<n;i++)
{
for(j=i+1;i<n;i++)
{
if(a[i]+a[j]==b)
{cout<<"{"<<a[i]<<","<<a[j]<<"}"<<endl;
}
}
}
return 0;
}

sample input: 5 1 2 3 4 5 7 sample output: {2,5} {3,4}

this is how we could easily execute this problem.

 


answered Feb 6 by Aqib Muhammed