Playground/자바문제집

[백준] 1940번

미숫가루설탕많이 2023. 4. 5. 09:55

 

 
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());    // 갑옷의 번호
        int[] A = new int[N];
        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {   // 재료 배열
            A[i] = Integer.parseInt(st.nextToken());
        }

        Arrays.sort(A); // 재료 배열 정렬
        int count = 0, i = 0, j = N - 1;

        while (i < j) {
            if (A[i] + A[j] < M) {
                i++;
            } else if (A[i] + A[j] > M) {
                j--;
            } else {
                count++;
                i++;
                j--;
            }
        }
        System.out.println(count);
    }
}

'Playground > 자바문제집' 카테고리의 다른 글

[백준] 2750번  (0) 2023.04.06
[백준] 12891번  (0) 2023.04.05
[백준] 2018번  (0) 2023.04.04
[프로그래머스] 숫자의 표현  (0) 2023.03.30
[프로그래머스] 이진 변환 반복하기  (0) 2023.03.30