Sum pair problem

my code is perfect but I do not get the efficient code it is of the order of an o(n^2) …can you plz give me an efficient code

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	if(1 <= n && n <= 1000)
	{
	int[] arr = new int[n];
	for(int i =0 ;i <n ;i++)
	{
		arr[i] = sc.nextInt();
	}
	int t = sc.nextInt();

	target(arr,t);
	}
}

public static void target(int[] arr, int t)
{
	for(int i =0 ;  i < arr.length -1 ;i++)
	{
		for(int j = i+1 ; j < arr.length ; j++)
		{
			if( arr[i] + arr[j] == t )
			{
				System.out.print(arr[i] +" and "+arr[j]);
				System.out.println();
			}
		}
	}
}

hey @mayanktiwari6957
1.First sort the given array.
2.Now take two variables one as left and other as right starting from 0th and end index of the sorted array respectively.
3.Now iterate till left<right
3.1 Calculate the sum of the elements at left and right position
If the sum is equal to the target then print both the elements. //printing target sum pairs
If the sum is less than the target then increase the left by 1
Else decrease the right by 1
4.End loop.Thus, all the pairs has been printed .

still a i am not able to do can u plz send me the code

public static void targetSum(int[] arr, int target){
        Arrays.sort(arr);
        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--;
            }
        }
    }

still my fourth test case does not pass…import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if( n> 1 && n<1000) { int[] arr = new int[n]; for(int i = 0 ; i < arr.length ; i++) { arr[i] = sc.nextInt(); } int target = sc.nextInt(); targetSum(arr,target); } } public static void insertion(int[] arr ) { for(int counter =1 ; counter <= arr.length-1 ; counter++) { int val = arr[counter]; int j = counter - 1 ; while(j>=0 && arr[j] > val ) { arr[j+1] = arr[j]; j–; } arr[j+1] = val; } } public static void targetSum(int[] arr, int target){ insertion(arr); 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–; } } } }

hey @mayanktiwari6957
just a change
while (left < right) instead of while (left <= right)
import java.util.*;

public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n > 1 && n < 1000) {
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
int target = sc.nextInt();
targetSum(arr, target);
}
}

public static void insertion(int[] arr) {
	for (int counter = 1; counter <= arr.length - 1; counter++) {
		int val = arr[counter];
		int j = counter - 1;
		while (j >= 0 && arr[j] > val) {
			arr[j + 1] = arr[j];
			j--;
		}
		arr[j + 1] = val;
	}
}

public static void targetSum(int[] arr, int target) {
	insertion(arr);
	
	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--;
		}
	}
}

}