Skip to content

Commit d3957e7

Browse files
Create FindSmallestLetterGreaterThanTarget.java
ZoranPandovski#3459 Added data-Structure program.
1 parent f38c647 commit d3957e7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.
2+
//Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.
3+
4+
class FindSmallestLetterGreaterThanTarget {
5+
public char nextGreatestLetter(char[] letters, char target) {
6+
int start = 0; //starting index of an array
7+
int end = letters.length-1; //ending index of an array
8+
while(start <= end){ //if start is less than or equal to end
9+
int mid = start + (end - start)/2; //find middle index so it becomes easy to find target element.
10+
if(target < letters[mid]){ //if tareget is less than middle elemant than we will find in left side of array
11+
end = mid - 1;
12+
}else{ //if tareget is greater than middle elemant than we will find in right side of array
13+
start = mid + 1;
14+
}
15+
}
16+
return letters[start % letters.length];
17+
}
18+
}

0 commit comments

Comments
 (0)