Not satisfying all test cases

The code given below is not satisfying all test cases.

// Array - Target Sum Pair

#include

using namespace std;

int main() {

int n;
cin >> n;
int a[1000] ;

for(int i=0; i<n; i++){
	cin >> a[i] ;
}

int target;
cin >> target;

for(int i=0; i<n; i++){
	for(int j=i; j<=n; j++){
		int sum = a[i] + a[j] ;

		if(sum == target){
			if(a[i] > a[j]){
				cout << a[j] << " and "<<a[i] <<endl;
			}
			else{
				cout << a[i]<< " and "<<a[j] <<endl;
			}
		}
	}
}


return 0;

}

hello @arshia27

start j from i+1 and go till j < n

also sort ur array otherwise due to difference in output order ,u will get wrong answer

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.