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.

Note that the letters wrap around.


	For example, if target == 'z' and letters == ['a', 'b'], the answer is 'a'.


&nbsp;
Example 1:

Input: letters = ["c","f","j"], target = "a"
Output: "c"


Example 2:

Input: letters = ["c","f","j"], target = "c"
Output: "f"


Example 3:

Input: letters = ["c","f","j"], target = "d"
Output: "f"


&nbsp;
Constraints:


	2 &lt;= letters.length &lt;= 104
	letters[i] is a lowercase English letter.
	letters is sorted in non-decreasing order.
	letters contains at least two different characters.
	target is a lowercase English letter.


class Solution:
    def nextGreatestLetter(self, letters: List[str], target: str) -> str:
        beg = 0
        end = len(letters)-1
        while beg <= end:
            mid = (beg+end)//2
            if letters[mid]>target:
                end = mid -1
            else:
                beg = mid +1
        return letters[beg] if beg<len(letters) else letters[0]
class Solution {
    public char nextGreatestLetter(char[] letters, char target) {
        int start=0,end=letters.length-1;
    while(start<=end){
      int mid=start+(end-start)/2;
      if(letters[mid]>target){    //strictly greater is the solution we want
        end = mid-1;
      }else{
        start=mid+1;
      }
    }
    return letters[start % letters.length];    // this is the wrap around condition , we use modulo %
    }
}
class Solution {
public:
    char nextGreatestLetter(vector<char>& letters, char target) {

        int siz = letters.size();
        bool isPresent = false;
        char ans;
        char temp = target;

        if (target == letters[siz-1]) return letters[0];

        for(int i=0; i<siz-1; i++) {
            if (target == letters[i] && target != letters[i+1]) {
                ans = letters[i+1];
                isPresent = true;
                break;
            }
        }

        // if target not in letters
        while (!isPresent) {
            temp = temp +1;
            isPresent = binary_search(letters.begin(), letters.end(), temp); // used STL !!
            if(isPresent) {
                ans=temp;
                break;
            }
        }
        return ans;
    }
};
/**
 * @param {character[]} letters
 * @param {character} target
 * @return {character}
 */
var nextGreatestLetter = function(letters, target) {
    let result=[]
    for (letter of letters){
        if (letter>target){
            result.push(letter);
        }
    }
    if (result.length){
        return result[0];
    }
    else{
        return letters[0];
    }
    
};
Find Smallest Letter Greater Than Target