[java] 백준 - 주몽

동훈·2024년 3월 11일


백준 1940번

앞서 풀었던 2018번 문제랑 비슷하게 투 포인터로 문제해결

package inflearn.B;

import java.util.Arrays;
import java.util.Scanner;

public class B_1940 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        int [] arr = new int[N];
        int count = 0;
        int first_index = 0;
        int last_index = N-1;
        for(int i = 0; i< N ;i++){
            arr[i] = sc.nextInt();
        }
        Arrays.sort(arr);

        while (first_index<last_index){
            if (arr[first_index] + arr[last_index] == M){
                first_index++;
                last_index--;
                count++;
            } else if (arr[first_index] + arr[last_index] < M) {
                first_index++;

            } else if (arr[first_index] + arr[last_index] > M) {
                last_index--;
            }else
                break;
        }
        System.out.println(count);


    }
}

맨날 temp 변수 써서 정렬 했는데 ..
Array.sort() 를 이제 알아버렸네
하.. 공부좀 하자 동훈아

profile
성실함 한스쿱

0개의 댓글