그 후 처음에 포인트 한 개, 마지막에 포인트 한 개를 두고 그 합을 M과 비교를 하면 된다.
start와 end 포인트가 있다고 가정해보자.
arr[start] + arr[end] > M 이면 오름차순이므로 end--를 해준다.
그 반대의 경우라면 start++를 해준다.
만약 두 수의 합이 M과 같다면 count++와 함께 start++, end--를 같이 해준다.
아래 코드를 보자.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int M = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] arr = new int[N];
for(int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
int i = 0;
int j = N - 1;
int count = 0;
while(i < j) {
if(arr[i] + arr[j] > M) {
j--;
} else if(arr[i] + arr[j] < M) {
i++;
} else {
count++;
i++;
j--;
}
}
System.out.println(count);
}
}