forked from srinathr91/TestJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAns1.java
More file actions
48 lines (43 loc) · 770 Bytes
/
Ans1.java
File metadata and controls
48 lines (43 loc) · 770 Bytes
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
41
42
43
44
45
46
47
48
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Ans1 {
static int search(List<Integer>a,int b){
int start=0;
int end=a.size()-1;
int result=-1;
if(b<a.get(0)){
return start;
}
else if(b>a.get(end)){
return end;
}
while(start<=end){
int index=start+((end-start)/2);
if(a.get(index)==b){
result=index;
end=index-1;
}
else if(a.get(index)>b){
end=index-1;
}
else{
start=end+1;
}
}
if(result==-1){
return end+1;
}else{
return result;
}
}
public static void manin(String[] args){
Scanner sc=new Scanner(System.in);
List<Integer> ip = new ArrayList<>();
while(sc.hasNextInt()){
ip.add(sc.nextInt());
}
search(ip,5);
sc.close();//tr
}
}