2 test cases are giving wrong result

my code is running correct in code blocks but in hacker blocks it is showing error in 2 test cases pls can u tell what is wrong in my code.
#include
using namespace std;
int main(){
int n,a[1000],sum,ans;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
cin>>sum;
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
ans=a[i]+a[j];
if(ans==sum){
cout<<a[i]<<" and "<<a[j]<<endl;
}
}
}

return 0;

}

In order to pass all the test cases, the output pairs generated must be in sorted order. So you have to sort the array first. You can then perform a check for target sum using 2 pointer approach in O(n) .There may be a hint video on this. You can refer that.

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.