# Smallest alphabet greater than a given character

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’.

**Examples**:

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.

## CPP

`/* C++ Program to find the smallest character ` ` ` `from the given set of letter, which is greater ` ` ` `than the target element */` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* Returns the smallest character from the given ` ` ` `set of letters that is greater than K */` `char` `nextGreatestAlphabet(vector<` `char` `>& alphabets, ` ` ` `char` `K) ` `{ ` ` ` ` ` `int` `l = 0, r = alphabets.size() - 1; ` ` ` ` ` `// Take the first element as l and ` ` ` `// the rightmost element as r ` ` ` `while` `(l < r && alphabets[r] > K) { ` ` ` ` ` `// if this while condition does not satisfy ` ` ` `// simply return the first element. ` ` ` `int` `mid = (l + r) / 2; ` ` ` `if` `(alphabets[mid] > K) ` ` ` `r = mid; ` ` ` `else` ` ` `l = mid + 1; ` ` ` `} ` ` ` ` ` `// Return the smallest element ` ` ` `return` `alphabets[l]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `vector<` `char` `> letters{ ` `'A'` `, ` `'K'` `, ` `'S'` `}; ` ` ` `char` `K = ` `'L'` `; ` ` ` `char` `result = nextGreatestAlphabet(letters, K); ` ` ` `cout << result << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`/* Java Program to find the smallest character ` `from the given set of letter, which is greater ` `than the target element */` ` ` `class` `GFG { ` ` ` ` ` `/* Returns the smallest character from the given ` ` ` `set of letters that is greater than K */` ` ` `static` `char` `nextGreatestAlphabet(` `char` `alphabets[], ` ` ` `char` `K) ` ` ` `{ ` ` ` ` ` `int` `l = ` `0` `, r = alphabets.length - ` `1` `; ` ` ` ` ` `// Take the first element as l and ` ` ` `// the rightmost element as r ` ` ` `while` `(l < r && alphabets[r] > K) ` ` ` `{ ` ` ` ` ` `// if this while condition does not ` ` ` `// satisfy simply return the first ` ` ` `// element. ` ` ` `int` `mid = (l + r) / ` `2` `; ` ` ` `if` `(alphabets[mid] > K) ` ` ` `r = mid; ` ` ` `else` ` ` `l = mid + ` `1` `; ` ` ` `} ` ` ` ` ` `// Return the smallest element ` ` ` `return` `alphabets[l]; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String [] args) ` ` ` `{ ` ` ` `char` `letters[] = { ` `'A'` `, ` `'K'` `, ` `'S'` `}; ` ` ` `char` `K = ` `'L'` `; ` ` ` `char` `result = ` ` ` `nextGreatestAlphabet(letters, K); ` ` ` ` ` `System.out.println(result); ` ` ` `} ` `} ` ` ` `// This code is contributed by Smitha. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 Program to find the smallest ` `# character from the given set of letter, ` `# which is greater than the target ` `# element */ ` ` ` `# Returns the smallest character from ` `# the given set of letters that is ` `# greater than K ` `def` `nextGreatestAlphabet(alphabets, K): ` ` ` ` ` `l ` `=` `0` ` ` `r ` `=` `len` `(alphabets) ` `-` `1` ` ` ` ` `# Take the first element as l and ` ` ` `# the rightmost element as r ` ` ` `while` `(l < r ` `and` `alphabets[r] > K) : ` ` ` ` ` `# if this while condition does ` ` ` `# not satisfy simply return the ` ` ` `# first element. ` ` ` `mid ` `=` `int` `((l ` `+` `r) ` `/` `2` `) ` ` ` ` ` `if` `(alphabets[mid] > K): ` ` ` `r ` `=` `mid ` ` ` `else` `: ` ` ` `l ` `=` `mid ` `+` `1` ` ` ` ` `# Return the smallest element ` ` ` `return` `alphabets[l] ` ` ` `# Driver Code ` `letters ` `=` `[` `'A'` `, ` `'K'` `, ` `'S'` `] ` `K ` `=` `'L'` `result ` `=` `nextGreatestAlphabet(letters, K) ` `print` `(result) ` ` ` `# This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

## C#

`/* C# Program to find the smallest character ` `from the given set of letter, which is greater ` `than the target element */` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `/* Returns the smallest character from the given ` ` ` `set of letters that is greater than K */` ` ` `static` `char` `nextGreatestAlphabet(` `char` `[]alphabets, ` ` ` `char` `K) ` ` ` `{ ` ` ` ` ` `int` `l = 0, r = alphabets.Length - 1; ` ` ` ` ` `// Take the first element as l and ` ` ` `// the rightmost element as r ` ` ` `while` `(l < r && alphabets[r] > K) ` ` ` `{ ` ` ` ` ` `// if this while condition does not ` ` ` `// satisfy simply return the first ` ` ` `// element. ` ` ` `int` `mid = (l + r) / 2; ` ` ` ` ` `if` `(alphabets[mid] > K) ` ` ` `r = mid; ` ` ` `else` ` ` `l = mid + 1; ` ` ` `} ` ` ` ` ` `// Return the smallest element ` ` ` `return` `alphabets[l]; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `char` `[]letters = { ` `'A'` `, ` `'K'` `, ` `'S'` `}; ` ` ` `char` `K = ` `'L'` `; ` ` ` `char` `result = ` ` ` `nextGreatestAlphabet(letters, K); ` ` ` ` ` `Console.Write(result); ` ` ` `} ` `} ` ` ` `// This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

Output:

S

The Time Complexity of the above approach is, **O(log N)** where N is the number of characters in the given set of Letters.

## Recommended Posts:

- Find a string such that every character is lexicographically greater than its immediate next character
- Lexicographically next greater string using same character set
- Smallest element greater than X not present in the array
- Smallest Greater (than S) String of length K whose letters are subset of S
- Lexicographically smallest string formed by removing at most 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
- Latin alphabet cipher
- Find letter's position in Alphabet using Bit operation
- Count characters at same position as in English alphabet
- Longest Common Prefix using Character by Character Matching
- Replace every character of string by character whose ASCII value is K times more than it
- Count substrings that starts with character X and ends with character Y
- Map every character of one string to another such that all occurrences are mapped to the same character

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.