Taregt sum pair

My code is not able to clear test case 2. Please help me find out the mistake in my code :
#include
#include
using namespace std;

int main()
{
int n;
cin>>n;
int a[n] = {0};
for(int i=0; i<n; i++)
{
cin>>a[i];
}
int target;
cin>>target;

sort(a,a+n);
int left=a[0];
int right=a[n-1];
while(left<right)
{
	if(left+right==target)
	{
		cout<<left<<" and "<<right<<endl;
		right--;
		left++;
	}
	else if(left+right>target)
	{
		right--;
	}
	else if(left+right<target)
	{
		left++;
	}
}
return 0;

}

hello @shreyasdaga
left and right should be the array indices in place of values .

code for refrence->

 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] << endl;
            left++;
            right--;
        }
    }
}