Given a list of sorted characters consisting of both Uppercase and Lowercase Alphabets and a particular target value, say K, the task is to find the smallest element in the list that is larger than K.
Letters also wrap around. For example, if K = ‘z’ and letters = [‘A’, ‘r’, ‘z’], then the answer would be ‘A’.
Input : Letters = ["D", "J", "K"] K = "B" Output: 'D' Explanation: The Next greater character of "B" is 'D' since it is the smallest element from the set of given letters, greater than "B". Input: Letters = ["h", "n", "s"] K = "t" Output: 'h'
Prerequisites: Binary Search
Approach: Binary Search can be applied to find the index of the smallest character in the given Set of Letters such that the character at that index is greater than K. If the element at the current mid is smaller than or equal to K, binary search is applied on the Right half, else it is applied on the left half.
The Time Complexity of the above approach is, O(log N) where N is the number of characters in the given set of Letters.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if frequency of each character is equal to its position in English Alphabet
- Find a string such that every character is lexicographically greater than its immediate next character
- Lexicographically next greater string using same character set
- Smallest number greater than X which is K-periodic
- Smallest element greater than X not present in the array
- Smallest number greater than Y with sum of digits equal to X
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Smallest Greater (than S) String of length K whose letters are subset of S
- Queries for Nth smallest character for a given range in a string
- Lexicographically smallest string formed by removing at most one character
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Frequency of smallest character in first sentence less than that of second sentence
- Latin alphabet cipher
- Find the most valued alphabet in the String
- Longest Common Prefix using Character by Character Matching
- Modify the string such that every character gets replaced with the next character in the keyboard
- Count substrings that starts with character X and ends with character Y
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Smitha Dinesh Semwal