TLE error for 3 test cases

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 sum = scn.nextInt();
	bubbleSort(arr);
	for (int i=0;i<arr.length-2;i++){
		int f = sum-arr[i];
		int j = i+1;
			int lo = j;
			int hi = arr.length-1;
			while(lo<hi){
				int mid = (lo+ hi)/2;
				if (arr[mid]==f){
					System.out.println(arr[i]+" and "+arr[mid] );
					break;
				}
				else if(arr[mid]>f){
					hi = mid;
				}
				else{
					lo=mid;
				}
			}
		}
	}
public static void bubbleSort(int[] arr){
	for (int counter =0;counter<arr.length-1;counter++){
		for (int i=0;i<arr.length-1-counter;i++){
			if(arr[i]>arr[i+1]){
				int temp = arr[i+1];
				arr[i+1]=arr[i];
				arr[i]=temp;
			}
		}
	}
}

}

I am receiving this error, this code passed the first test case but for the rest of the test cases it is receiving this error.

follow this to handle the tle

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--;
            }
        }
    }