Skip to content

Commit 1c028a0

Browse files
Create binarysearch.java
1 parent 5e9d1dc commit 1c028a0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/binarysearch.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
3+
public class binarysearch {
4+
public static void main(String[] jah) {
5+
Scanner sc = new Scanner(System.in);
6+
int size = sc.nextInt();
7+
int q = sc.nextInt();
8+
int[] arr = new int[size];
9+
for (int i = 0; i < size; i++) {
10+
arr[i] = sc.nextInt();
11+
}
12+
Arrays.sort(arr);
13+
while (q > 0) {
14+
boolean isfound = false;
15+
int x = sc.nextInt();
16+
int start = 0;
17+
int end = size - 1;
18+
while(start<=end) {
19+
20+
int mid = (start + end) / 2;
21+
if (arr[mid] > x) {
22+
end = mid - 1;
23+
} else if (arr[mid] < x) {
24+
start = mid + 1;
25+
} else {
26+
System.out.println("found"+" "+(mid));
27+
isfound = true;
28+
break;
29+
}
30+
}
31+
q--;
32+
if (!isfound) {
33+
System.out.println("not found");
34+
}
35+
}
36+
sc.close();
37+
}
38+
}

0 commit comments

Comments
 (0)