Why only 1 test case is passed

#include

using namespace std;

int main()
{
int n,target;
int i=0;
int j=n-1;
cin>>n;
int a[n];
for(i=0; i<n; i++){
cin>>a[i];
}
cin>>target;
while(j>=i){
for( i=0; i<n; i++){
for( j=n-1; j>0;j–) {
if(a[i]+a[j]==target){
cout<<a[i]<<endl;
cout<<a[j]<<endl;
j–;
i++;
}
}
}

}

return 0;

}

hi @rathidevesh906_1dcc73a061c159bb
u have to print smaller element first followed by larger element…
refer this code–>

#include<iostream>
#include<algorithm>
using namespace std;

int main() {
	int n,s;
	cin>>n;
	int a[1000];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int x;
	cin>>x;
	for(int i = 0; i<=n-1; i++){
        for(int j =i+1; j <=n-1; j++){
            s = a[i]+a[j];
            if(s == x){
                cout<<min(a[i], a[j])<<" and "<<max(a[i], a[j])<<endl;
            }
        }
    }
	return 0;
}

That’s not a problem sir when I entered
6
1
2
3
4
5
6
6
[my output is->1 and 5
3 and 3] so why this happen why not it gives me ans 2 and 4

you will have to run your inner loop (ie for j) till j > i and not j > 0

1 Like

Now it is running properly

okay great.
Kindly mark the doubt as resolved :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.