Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22.03.09 - [BOJ] 1920. 수 찾기 #223

Closed
suhyunsim opened this issue Mar 9, 2022 · 0 comments
Closed

22.03.09 - [BOJ] 1920. 수 찾기 #223

suhyunsim opened this issue Mar 9, 2022 · 0 comments
Assignees
Labels
성공 맞은 문제 실버 BOJ - 실버 이분탐색

Comments

@suhyunsim
Copy link
Owner

문제

핵심 아이디어

  • 기본적인 이분탐색 문제

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q1920;

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

public class Main {
    static int[] arr;
    static int m;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        Arrays.sort(arr);
        m = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < m; i++) {
            if (binarySearch(sc.nextInt()) >= 0) {
                sb.append(1).append('\n');
            } else {
                sb.append(0).append('\n');
            }
        }
        System.out.println(sb);
    }

    private static int binarySearch(int target) {
        int start = 0;
        int end = arr.length - 1;
        while (start <= end) {
            int mid = (start + end) / 2;
            if (arr[mid] < target) {
                start = mid + 1;
            } else if (arr[mid] > target) {
                end = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
성공 맞은 문제 실버 BOJ - 실버 이분탐색
Projects
None yet
Development

No branches or pull requests

1 participant