투 포인터 - 주몽의 명령

김동헌·2023년 10월 30일

Algorithm

목록 보기
5/13
post-thumbnail

🔎 문제

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.StringTokenizer;

public class BackJoon_1940 {
    public static void main(String[] args) throws IOException {
        
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(bf.readLine());
        int M = Integer.parseInt(bf.readLine());
        int[] A = new int[N];

        StringTokenizer st = new StringTokenizer(bf.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 if((A[i] + A[j]) == M) {
                count++;
                i++;
                j--;
            }
        }
        System.out.println(count);
    }
}
profile
백엔드 기록 공간😁

0개의 댓글