[백준] PS/Java [실랜디]

[백준 1940] 주몽 - JAVA

SH3542 2025. 3. 6. 18:42

https://www.acmicpc.net/problem/1940

 

투포인터 기본

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;

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());

    int[] a = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).sorted().toArray();

    int ans = 0;
    int l = 0;
    int r = N - 1;

    while (l < r) {

      int v = a[l] + a[r];

      if (v == M) {
        ans++;
        l++;
        r--;
      } else if (v < M) {
        l++;
      } else {
        r--;
      }
    }
    System.out.println(ans);
  }
}