Whats wrong in the code?

include

include

using namespace std;

void targetSum(int*,int,int);

int main()
{
int n,target;
cin>>n>>target;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
targetSum(arr,n,target);
return 0;
}
void targetSum(int arr[], int n,int target){
sort(arr,arr+n);
int left = 0;
int right = n - 1;
while (left < right) {
int sum = arr[left] + arr[right];
if (sum > target) {
right–;
} else if (sum < target) {
left++;
} else {
cout<<arr[left] << " and " << arr[right];
left++;
right–;
}
}
}
Output :

TESTCASE # 1 : wrong-answer (Time: 0 s)
TESTCASE # 2 : no-output (Time: 0 s)
TESTCASE # 3 : no-output (Time: 0 s)
TESTCASE # 4 : no-output (Time: 0 s)

Hi @rajatpillai10
You have to make 2 changes in your code.

  1. You have to take target input after taking input of all elements of the array.
  2. When you are printing the arr[left] and arr[right] you are missing a endl at the end.

Your corrected code :