[BOJ 2467] ์šฉ์•ก

 

1. ๋ฌธ์ œ : https://www.acmicpc.net/problem/2110

 

2. ํ’€์ด

ํˆฌํฌ์ธํ„ฐ ํ’€์ด, l, r ๋ชจ๋‘ ์ €์žฅํ•ด์ค˜์•ผํ•จ

 

3. ์ฝ”๋“œ

package baekjoon;

import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        long[] arr = new long[n];
        StringTokenizer st = new StringTokenizer(br.readLine());
        for(int i=0; i<n; i++) {
            arr[i] = Long.parseLong(st.nextToken());
        }

        int left =0;
        int right =n-1;
        int ml =0, mr = 0;
        long min = Long.MAX_VALUE;
        while(left<right) {
            long sum = arr[left]+arr[right];
            if(min > Math.abs(sum)) {
                min = Math.abs(sum);
                ml = left; mr = right;
            }
            if(sum>=0) {
                right--;
            }else {
                left++;
            }
        }
        System.out.println(arr[ml] +" "+arr[mr]);
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ