Nearest smaller character to a character K from a Sorted Array
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: t
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++
#include <bits/stdc++.h>
using namespace std;
char bs( char ar[], int n, int ele)
{
int start = 0;
int end = n - 1;
char ch = '@' ;
while (start <= end) {
int mid = start + (end - start) / 2;
if (ar[mid] == ele)
end = mid - 1;
else if (ar[mid] < ele) {
ch = ar[mid];
start = mid + 1;
}
else
end = mid - 1;
}
return ch;
}
int main()
{
char ar[] = { 'e' , 'g' , 't' , 'y' };
int n = sizeof (ar) / sizeof (ar[0]);
char K = 'u' ;
char ch = bs(ar, n, K);
if (ch == '@' )
cout << "-1" ;
else
cout << ch;
return 0;
}
|
Java
import java.util.*;
class GFG{
static char bs( char ar[], int n, int ele)
{
int start = 0 ;
int end = n - 1 ;
char ch = '@' ;
while (start <= end)
{
int mid = start + (end - start) / 2 ;
if (ar[mid] == ele)
end = mid - 1 ;
else if (ar[mid] < ele)
{
ch = ar[mid];
start = mid + 1 ;
}
else
end = mid - 1 ;
}
return ch;
}
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);
}
}
|
Python3
def bs(a, n, ele):
start = 0
end = n - 1
ch = '@'
while (start < = end):
mid = start + (end - start) / / 2 ;
if (ar[mid] = = ele):
end = mid - 1
elif (ar[mid] < ele):
ch = ar[mid]
start = mid + 1 ;
else :
end = mid - 1 ;
return ch
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)
|
C#
using System;
class GFG{
static char bs( char []ar, int n, int ele)
{
int start = 0;
int end = n - 1;
char ch = '@' ;
while (start <= end)
{
int mid = start + (end - start) / 2;
if (ar[mid] == ele)
end = mid - 1;
else if (ar[mid] < ele)
{
ch = ar[mid];
start = mid + 1;
}
else
end = mid - 1;
}
return ch;
}
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);
}
}
|
Javascript
<script>
function bs(ar, n, ele)
{
let start = 0;
let end = n - 1;
let ch = '@' ;
while (start <= end)
{
let mid = start + Math.floor((end - start) / 2);
if (ar[mid] == ele)
end = mid - 1;
else if (ar[mid] < ele)
{
ch = ar[mid];
start = mid + 1;
}
else
end = mid - 1;
}
return ch;
}
let ar = [ 'e' , 'g' , 't' , 'y' ];
let n = ar.length;
let K = 'u' ;
let ch = bs(ar, n, K);
if (ch == '@' )
document.write( "-1" );
else
document.write(ch);
</script>
|
Time Complexity: O(logN)
Auxiliary Space: O(1)
Last Updated :
28 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...