BackJoon Algorithm 단어 퍼즐 9946 (Java)

업데이트:
최대 1 분 소요

BackJoon Algorithm - Java

alt

문제

alt

풀이

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

public class Back_1940 {
    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;
        int i = 0;
        int 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);
        br.close();
    }
}


댓글남기기