My code works in the ide but I'm unable to figure out the problem as to why it 2 test cases have a wrong result with my code

this is my code
#include
using namespace std;
int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{cin>>a[i];}
int sum;
cin>>sum;

for(int i=0;i<n;i++)
{
    for(int j=i+1;j<n;j++)
    {
        if(a[i]+a[j]==sum)
        {
            cout<<a[i]<<" and "<<a[j]<<endl;
        }
    }
}
return 0;

}

According to the question, you have to print the result in sorted order.
This approach of yours will not print in sorted order.