What is my mistake in this?

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

int left = 0;
int right = n-1;
int target;
cin>>target;

while(left<right){
	int sum = a[left] + a[right];
	if(sum>target){
		right--;
	}
	else if(sum<target){
		left++;
	}
	else if(sum==target){
		cout<< a[left]<< " and "<<a[right];
		left++;
		right--;		
	}
}

return 0;

}

hi @kartikayasija7057
firstly u have to sort the array to use 2 pointer approach… then according to array u have to print smaller element first then the larger one…
corrected code --> https://ide.codingblocks.com/s/642775
its passing all test cases now…

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.