forked from shivanshjaitly/Hackerrank-java-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.java
41 lines (38 loc) · 1.12 KB
/
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
40
41
// FIND SMALLEST LETTER GREATER THAN TARGET.
/*Given a characters array letters that is sorted in
non-decreasing order and a character target,
return the smallest character in the array that is
larger than target. */
public class BinarySearch {
public static void main(String[] args) {
char[] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
char target = 'j';
System.out.println(findChar(letters, target));
}
static char findChar(char[] letters, char target)
{
int start = 0;
int end = (letters.length - 1);
if(target >= letters[end] || target <= letters[start])
{
return letters[start];
}
while(start <= end)
{
int mid = start + (end - start) /2;
if( target == letters[mid])
{
return letters[mid+1];
}
else if(target < letters[mid])
{
end = (char) (mid - 1);
}
else
{
start = (char) (mid + 1);
}
}
return letters[start];
}
}