문제 풀이/백준

[백준] 1940번. 주몽 (JAVA)

27200 2024. 1. 28. 17:00

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


문제


풀이

import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int[] arr = new int[N];

        st = new StringTokenizer(br.readLine());
        int M = Integer.parseInt(st.nextToken());

        st = new StringTokenizer(br.readLine());
        for(int i = 0; i < N; i++){
            arr[i] = Integer.parseInt(st.nextToken());
        }

        Arrays.sort(arr);

        int i = 0, j = N - 1;
        int count = 0;

        while(i < j){
            if(arr[i] + arr[j] > M){
                j--;
            }else if(arr[i] + arr[j] < M){
                i++;
            }else{
                count++;
                i++;
                j--;
            }
        }

        System.out.println(count);

    }

}

 

투포인터를 이용하기 위해 정렬을 사용하여 풀면 되는 문제이다