https://www.acmicpc.net/problem/3273
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class BOJ_3273_두수의합 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] line = br.readLine().split(" ");
int x = Integer.parseInt(br.readLine());
int[] arr = new int[n];
for (int i = 0; i < line.length; i++) {
arr[i] = Integer.parseInt(line[i]);
}
Arrays.sort(arr);
int left = 0;
int right = 1;
int result = 0;
while (left < n - 1) {
while (right < n - 1) {
if (arr[right] + arr[left] != x) {
right++;
} else {
left++;
result++;
break;
}
}
}
System.out.println(result);
}
}
public class BOJ_3273_두수의합 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] line = br.readLine().split(" ");
int x = Integer.parseInt(br.readLine());
int[] arr = new int[n];
for (int i = 0; i < line.length; i++) {
arr[i] = Integer.parseInt(line[i]);
}
Arrays.sort(arr);
int left = 0;
int right = 1;
int result = 0;
while (left < n - 1) {
if (arr[right] + arr[left] < x) {
right++;
} else if (arr[right] + arr[left] == x) {
left++;
right = left + 1;
result++;
} else {
left++;
}
}
System.out.println(result);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class BOJ_3273_두수의합 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] line = br.readLine().split(" ");
int x = Integer.parseInt(br.readLine());
int[] arr = new int[n];
for (int i = 0; i < line.length; i++) {
arr[i] = Integer.parseInt(line[i]);
}
Arrays.sort(arr);
int left = 0, right = 1, result = 0;
while (left < n - 1) {
if (right >= n) { //right 가 끝에 닿으면 재설정
left++;
right = left + 1;
continue;
}
int sum = arr[left] + arr[right];
if (sum == x) {
left++;
result++;
right = left + 1;
} else if (sum < x) {
right++;
} else {
left++;
right = left + 1;
}
}
System.out.println(result);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class BOJ_3273_두수의합 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] line = br.readLine().split(" ");
int x = Integer.parseInt(br.readLine());
int[] arr = new int[n];
for (int i = 0; i < line.length; i++) {
arr[i] = Integer.parseInt(line[i]);
}
Arrays.sort(arr);
int left = 0, right = n-1, result = 0;
while (left < right) {
int sum = arr[left] + arr[right];
if (sum == x) {
left++;
right--; // 중복 없음으로 left 를 증가시켰음으로 sum ==x 되려면 Right 를 감소시켜야 hit 확률 up
result++;
} else if (sum < x) {
left++;
} else { //더 크다면
right--;
}
}
System.out.println(result);
}
}