Smallest Chatacter | tech::interview
Given a sorted character array and a character, return the smallest character that is strictly larger than the search character.
If no such character exists, return the smallest character in the array.
For example:
Input:
Output:
Input:
Output:
Input:
Output:
Input:
Output:
Binary Search:
Given a sorted character array and a character, return the smallest character that is strictly larger than the search character.
If no such character exists, return the smallest character in the array.
For example:
Input:
['c', 'f', 'j', 'p', 'v'], 'a'
Output:
'c'
Input:
['c', 'f', 'j', 'p', 'v'], 'c'
Output:
'f'
Input:
['c', 'f', 'j', 'p', 'v'], 'z'
Output:
'c'
Input:
['c', 'c', 'k'], 'f'
Output:
'k'
Binary Search:
Read full article from Smallest Chatacter | tech::interviewpublic char smallest_character(String str, char c) {int l = 0, r = str.length() - 1;char ret = str.charAt(0);while(l <= r) {int m = l + (r-l) / 2;if(str.charAt(m) > c) {ret = str.charAt(m);r = m - 1;} else {l = m + 1;}}return ret;}