Target sum pair - what is the error?

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] arr = new int[n];
for(int i = 0 ; i< n ; i++){
arr[i] = scn.nextInt();

	}
	int target = scn.nextInt();

	int left = 0;
	int right = arr.length - 1;

	while (left < right) {

		int sum = arr[left] + arr[right];

		if (sum > target) {
			right--;
		} else if (sum < target) {
			left++;
		} else {
			System.out.println(arr[left] + " and " + arr[right]);
			left++;
			right--;
		}

	}
	

}

}

@asthaaggarwal2
you need to sort the array first.
using Arrays.sort();

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.