-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinarySearch.java
39 lines (34 loc) · 1014 Bytes
/
BinarySearch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
}
int k = input.nextInt();
/*
* Given that a[0] <= a[1] <= ... <= a[n - 1]
* Sorted in non-decreasing order
* Check whether `k` exists in `a` or not
*/
System.out.println(doesExist(a, k) ? "YES" : "NO");
input.close();
}
private static boolean doesExist(int[] a, int toFind) {
int low = 0, high = a.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (a[mid] == toFind) {
return true;
}
if (a[mid] > toFind) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return false;
}
}