排序+双指针

package org.niuke.solution44;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] ints = new int[n];
        for(int i = 0; i < n; i++){
            ints[i] = scanner.nextInt();
        }
        int need = scanner.nextInt();
        Arrays.sort(ints);
        boolean flag=false;
        int l = 0, r = ints.length - 1;
        while (l < r) {
            if(ints[l] + ints[r] > need){
                r--;
            }else if(ints[l] + ints[r] < need){
                l++;
            }else{
                System.out.println(ints[l] + " " + ints[r]);
                l++;
                flag=true;
            }
        }
        if(!flag){
            System.out.println("NO");
        }
    }
}