Take as input N, the size of array. Take N more inputs and store that in an array. Take as input “target”, a number. Write a function which prints all pairs of numbers which sum to target. Input Format: Constraints: Length of the arrays should be between 1 and 1000. output format sample input 5 1 3 4 2 5 5 sample output 1 and 4 2 and 3
code is running but all test cases are not being
passed,suggest some better way of doing it
#include
#include
#include
using namespace std;
bool compare(int a,int b)
{
return a>b;
}
void pairsum(int * a,int n,int target)
{
int j,k,ans=0,l[1000],m[1000],i=0,t,x=target/2;
for(k=0;k<n-1;k++)
{
for(j=k+1;j<n;j++)
{
ans=a[k]+a[j];
if(ans==target)
{
l[i]=a[k];
m[i]=a[j];
i++;
}
}
}
sort(l,l+i);
sort(m,m+i,compare);
for(t=0;t<i;t++)
{
cout<<l[t]<<" “;
cout<<m[t]<<” ";
}
cout<<endl;
for(t=0;t<i;t++)
{
if(t==0)
{
cout<<l[t]<<" "<<"and"<<" "<<m[t];
cout<<endl;
}
if(l[t]==l[t+1])
{
continue;
}
else
{
cout<<l[t+1]<<" "<<"and"<<" "<<m[t+1];
cout<<endl;
}
}
}
int main()
{
int n,i;
cin>>n;
if(n>1000)
{
exit(0);
}
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
int target;
cin>>target;
pairsum(a,n,target);
return 0;
}