# Nearest smaller character to a character K from a Sorted Array

• Difficulty Level : Medium
• Last Updated : 28 Sep, 2021

Given a sorted array of characters arr[] and a character K, the task is to find the character with nearest smaller ASCII value than K from the given array. Print -1 if no character is found to be having smaller ASCII value than K.
Examples:

Input: arr[] = {‘e’, ‘g’, ‘t’, ‘y’}, K = ‘u’
Output:
Explanation:
The character with nearest smaller ASCII value as of ‘u’ is ‘t’.
Input: arr[] = {‘e’, ‘g’, ‘t’, ‘y’}, K = ‘a’
Output: -1
Explanation:
No character exists with ASCII value smaller than that of ‘a’.

Naive Approach:
The simplest approach to solve the problem is to iterate over the array and find the character having smaller ASCII value than that of K and the difference between their ASCII values is minimum.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach:
The idea is to use Binary Search to find out the floor element (largest character just smaller than key). Follow the steps below to solve the problem:

• Perform binary search on the array.
• Check if the current middle element(mid) is equal to the character K or not.
• If so, then set start to mid – 1, because we need to find just smaller element.
• If the arr[mid] is smaller than K, then store it in some variable say ch. Set start to mid + 1 to search for a smaller character nearer to K.
• Otherwise, set end to mid-1.
• Keep repeating the above steps. Finally, print the character obtained after completing the search. If no character is obtained, print -1.

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement``// the above approach``#include ``using` `namespace` `std;` `// Function to return the``// nearest smaller character``char` `bs(``char` `ar[], ``int` `n, ``int` `ele)``{``    ``int` `start = 0;``    ``int` `end = n - 1;` `    ``// Stores the nearest smaller``    ``// character``    ``char` `ch = ``'@'``;` `    ``// Iterate till starts cross end``    ``while` `(start <= end) {` `        ``// Find the mid element``        ``int` `mid = start + (end - start) / 2;` `        ``// Check if K is found``        ``if` `(ar[mid] == ele)``            ``end = mid - 1;` `        ``// Check if current character``        ``// is less than K``        ``else` `if` `(ar[mid] < ele) {``            ``ch = ar[mid];` `            ``// Increment the start``            ``start = mid + 1;``        ``}` `        ``// Otherwise``        ``else` `            ``// Increment end``            ``end = mid - 1;``    ``}``    ``// Return the character``    ``return` `ch;``}` `// Driver Code``int` `main()``{``    ``char` `ar[] = { ``'e'``, ``'g'``, ``'t'``, ``'y'` `};``    ``int` `n = ``sizeof``(ar) / ``sizeof``(ar);` `    ``char` `K = ``'u'``;` `    ``char` `ch = bs(ar, n, K);` `    ``if` `(ch == ``'@'``)``        ``cout << ``"-1"``;``    ``else``        ``cout << ch;` `    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``import` `java.util.*;` `class` `GFG{` `// Function to return the``// nearest smaller character``static` `char` `bs(``char` `ar[], ``int` `n, ``int` `ele)``{``    ``int` `start = ``0``;``    ``int` `end = n - ``1``;` `    ``// Stores the nearest smaller``    ``// character``    ``char` `ch = ``'@'``;` `    ``// Iterate till starts cross end``    ``while` `(start <= end)``    ``{` `        ``// Find the mid element``        ``int` `mid = start + (end - start) / ``2``;` `        ``// Check if K is found``        ``if` `(ar[mid] == ele)``            ``end = mid - ``1``;` `        ``// Check if current character``        ``// is less than K``        ``else` `if` `(ar[mid] < ele)``        ``{``            ``ch = ar[mid];` `            ``// Increment the start``            ``start = mid + ``1``;``        ``}` `        ``// Otherwise``        ``else` `            ``// Increment end``            ``end = mid - ``1``;``    ``}``    ` `    ``// Return the character``    ``return` `ch;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``char` `ar[] = { ``'e'``, ``'g'``, ``'t'``, ``'y'` `};``    ``int` `n = ar.length;` `    ``char` `K = ``'u'``;` `    ``char` `ch = bs(ar, n, K);` `    ``if` `(ch == ``'@'``)``        ``System.out.print(``"-1"``);``    ``else``        ``System.out.print(ch);``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to return the``# nearest smaller character``def` `bs(a, n, ele):``    ` `    ``start ``=` `0``    ``end ``=` `n ``-` `1``    ` `    ``# Stores the nearest smaller``    ``# character``    ``ch ``=` `'@'``    ` `    ``# Iterate till starts cross end``    ``while` `(start <``=` `end):``        ` `        ``# Find the mid element``        ``mid ``=` `start ``+` `(end ``-` `start) ``/``/` `2``;` `        ``# Check if K is found``        ``if``(ar[mid] ``=``=` `ele):``            ``end ``=` `mid ``-` `1``            ` `        ``# Check if current character``        ``# is less than K``        ``elif``(ar[mid] < ele):``            ``ch ``=` `ar[mid]``            ` `            ``# Increment the start``            ``start ``=` `mid ``+` `1``;``        ` `        ``# Otherwise``        ``else``:``            ` `            ``# Increment end``            ``end ``=` `mid ``-` `1``;``            ` `    ``# Return the character``    ``return` `ch` `# Driver code``if` `__name__``=``=``'__main__'``:``    ` `    ``ar ``=` `[ ``'e'``, ``'g'``, ``'t'``, ``'y'` `]``    ``n ``=` `len``(ar)``    ``K ``=` `'u'``;` `    ``ch ``=` `bs(ar, n, K);` `    ``if` `(ch ``=``=` `'@'``):``        ``print``(``'-1'``)``    ``else``:``        ``print``(ch)``        ` `# This code is contributed by rutvik_56`

## C#

 `// C# program to implement``// the above approach``using` `System;` `class` `GFG{` `// Function to return the``// nearest smaller character``static` `char` `bs(``char` `[]ar, ``int` `n, ``int` `ele)``{``    ``int` `start = 0;``    ``int` `end = n - 1;` `    ``// Stores the nearest smaller``    ``// character``    ``char` `ch = ``'@'``;` `    ``// Iterate till starts cross end``    ``while` `(start <= end)``    ``{` `        ``// Find the mid element``        ``int` `mid = start + (end - start) / 2;` `        ``// Check if K is found``        ``if` `(ar[mid] == ele)``            ``end = mid - 1;` `        ``// Check if current character``        ``// is less than K``        ``else` `if` `(ar[mid] < ele)``        ``{``            ``ch = ar[mid];` `            ``// Increment the start``            ``start = mid + 1;``        ``}` `        ``// Otherwise``        ``else` `            ``// Increment end``            ``end = mid - 1;``    ``}``    ` `    ``// Return the character``    ``return` `ch;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``char` `[]ar = { ``'e'``, ``'g'``, ``'t'``, ``'y'` `};``    ``int` `n = ar.Length;` `    ``char` `K = ``'u'``;``    ``char` `ch = bs(ar, n, K);` `    ``if` `(ch == ``'@'``)``        ``Console.Write(``"-1"``);``    ``else``        ``Console.Write(ch);``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

`t`

Time Complexity: O(logN)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up