Seize your moment! ๐Ÿ‘พ

์•ˆ๋…•ํ•˜์„ธ์š”. Eric์ž…๋‹ˆ๋‹ค. ์ œ ๋ธ”๋กœ๊ทธ์— ๋ฐฉ๋ฌธํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ป ๊ฐœ๋ฐœ๊ณต๋ถ€/Algorithm

[Eric's ๋ฐฑ์ค€] 3273๋ฒˆ - ๋‘ ์ˆ˜์˜ ํ•ฉ - Java

Eric_ko 2023. 1. 18. 09:23

 

ํˆฌํฌ์ธํ„ฐ ๋ฅผ ์ด์šฉํ•ด์„œ

๋ฌธ์ œ๋ฅผ ํ’€์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

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

public class N3273 {
    public static void main(String[] args) {
        N3273 T = new N3273();
        Scanner kb = new Scanner(System.in);
        int n = kb.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = kb.nextInt();
        }
        int x = kb.nextInt();
        System.out.println(T.solution(n, arr, x));
    }
    public int solution(int n, int[] arr, int x) {
        int answer = 0;
        Arrays.sort(arr);
        int lt = 0, rt = n - 1;
        while (lt < rt) {
            int sum = arr[lt] + arr[rt];
            if (sum == x) {
                answer++;
                lt++;
                rt--;
            } else if (sum > x) {
                rt--;
            }else {
                lt++;
            }
        }
        return answer;
    }
}

Solved.ac ํ”„๋กœํ•„