Test case 2 shows wrong answer

i cannot figure out my mistake
#include
using namespace std;
int main() {int n;
cin>>n;
int a[999];
for(int i=0;i<n;i++){
cin>>a[i];
}int goal;
cin>>goal;
for(int i=0;i<(n/2);i++){
for(int j=(n-1);j>=(n/2);j–){
int sum=a[i]+a[j];
if(sum==goal){if(a[j]>a[i]){
cout<<a[i]<<" β€œ<<β€œand”<<” β€œ<<a[j]<<endl;}
if(a[j]<a[i]){
cout<<a[j]<<” β€œ<<β€œand”<<” "<<a[i]<<endl;
}
}
}
}
return 0;
}

@varun1928.joshi please share your code using cb ide

@varun1928.joshi you have to do this question using one loop ie o(n) timr

but is this code wrong?

@varun1928.joshi maybe format is not correct try sorting the array first

that will further increase the number of loops though

my way seems pretty convinient

@varun1928.joshi yes code correct but for output format sort it before doing your algo


corrected

i see now it works but still i dont know rightly why it showed an error coz even now the code remains same except that sort line by you…

@varun1928.joshi it should have showed wrong answer as output was not matching