240620 좋다

Jongleee·2024년 6월 20일
0

TIL

목록 보기
604/737
public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int n = Integer.parseInt(br.readLine());

	int[] arr = new int[n];
	StringTokenizer st = new StringTokenizer(br.readLine());

	for (int i = 0; i < n; i++) {
		arr[i] = Integer.parseInt(st.nextToken());
	}

	Arrays.sort(arr);
	int count = 0;

	for (int i = 0; i < n; i++) {
		if (isGoodNumber(arr, i)) {
			count++;
		}
	}

	System.out.println(count);
}

static boolean isGoodNumber(int[] arr, int index) {
	int target = arr[index];
	int left = 0;
	int right = arr.length - 1;

	while (left < right) {
		if (left == index) {
			left++;
		} else if (right == index) {
			right--;
		} else {
			int sum = arr[left] + arr[right];
			if (sum == target) {
				return true;
			} else if (sum < target) {
				left++;
			} else {
				right--;
			}
		}
	}

	return false;
}

출처:https://www.acmicpc.net/problem/1253

0개의 댓글