본문 바로가기
알고리즘 & 자료구조/백준

백준 3273

by 신재권 2022. 9. 9.
package baekjoon.이분탐색;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

class Main3273 {

   static int N, X, ans;
   static int[] a;

   public static void main(String[] args) throws Exception {
      input();
      go();
      System.out.println(ans);
   }

   private static void go() {
      int L = 0, R = N - 1;
      while (L < R) {
         if (a[L] + a[R] == X) {
            ans++;
            L++;
            R--;
         } else if (a[L] + a[R] > X) {
            R--;
         } else if (a[L] + a[R] < X) {
            L++;
         }
      }
   }

   private static void input() throws IOException {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringTokenizer st;
      N = Integer.parseInt(br.readLine());
      a = new int[N];
      st = new StringTokenizer(br.readLine());
      for (int i = 0; i < N; i++) {
         a[i] = Integer.parseInt(st.nextToken());
      }
      X = Integer.parseInt(br.readLine());
      Arrays.sort(a);
   }
}

 

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 2805  (0) 2022.09.11
백준 10816  (0) 2022.09.09
백준 1764  (0) 2022.09.08
백준 15970  (0) 2022.09.07
백준 11652  (0) 2022.09.07