-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTest.java
More file actions
40 lines (34 loc) Β· 1.14 KB
/
Test.java
File metadata and controls
40 lines (34 loc) Β· 1.14 KB
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
40
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int size = s.nextInt();
int arr[] = new int[size];
for(int index =0 ; index<arr.length ; index++){
arr[index] = s.nextInt();
}
int element = s.nextInt();
int ans = parse(binarySearch(arr, element), element, arr[0]);
System.out.println(ans);
if(ans != -1) System.out.println(arr[ans]);
}
private static int parse(int index, int element, int first){
if(index == 0 && element < first)
return -1;
return index;
}
private static int binarySearch(int arr[], int element){
int tail, head;
for(tail =0, head = arr.length ; tail < head-1 ; ){
if(arr[(tail + head)/2] == element)
return (tail+head)/2;
else if(arr[(tail + head)/2] < element){
tail = (tail+head)/2;
}
else {
head = (head + tail)/2;
}
}
return (tail + head)/2;
}
}