Is it necessary to define the elements of sorted array in the code itself? Can’t we provide the inputs from console?
When I am trying to write the code for that , it’s not working. Please let me know, where am I going wrong?
#include
using namespace std;
int main()
{
int n;
cin>>n;
int a[1000];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int s;
cin>>s;
int i=0;
int j=sizeof(a)/sizeof(int)-1;
while(i<j)
{
int c_sum=a[i]+a[j];
if(c_sum>s)
{
j--;
}
else if(c_sum<s)
{
i++;
}
else if(c_sum==s)
{
cout<<a[i]<<" and"<<a[j]<<endl;
i++;
j--;
}
}
return 0;
}