Given an array arr[] of size N and an integer X. The task is to find the smallest element greater than X which is not present in the array.
Examples:
Input: arr[] = {1, 5, 10, 4, 7}, X = 4
Output: 6
6 is the smallest number greater than 4
which is not present in the array.
Input: arr[] = {1, 5, 10, 6, 11}, X = 10
Output: 12
Approach:
An efficient solution is based on binary search. First sort the array. Take low as zero and high as N – 1. And search for the element X + 1. If the element at mid ( which is (low+high)/2 ) is less than or equals to the searching element then make low as mid + 1. Otherwise, make high as mid – 1. If the element at mid gets equal to the searching element then increment the searching element by one and make high as N – 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Next_greater( int a[], int n, int x)
{
sort(a, a + n);
int low = 0, high = n - 1, ans = x + 1;
while (low <= high) {
int mid = (low + high) / 2;
if (a[mid] <= ans) {
if (a[mid] == ans) {
ans++;
high = n - 1;
}
low = mid + 1;
}
else
high = mid - 1;
}
return ans;
}
int main()
{
int a[] = { 1, 5, 10, 4, 7 }, x = 4;
int n = sizeof (a) / sizeof (a[0]);
cout << Next_greater(a, n, x);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int Next_greater( int a[], int n, int x)
{
Arrays.sort(a);
int low = 0 , high = n - 1 , ans = x + 1 ;
while (low <= high)
{
int mid = (low + high) / 2 ;
if (a[mid] <= ans)
{
if (a[mid] == ans)
{
ans++;
high = n - 1 ;
}
low = mid + 1 ;
}
else
high = mid - 1 ;
}
return ans;
}
public static void main(String[] args)
{
int a[] = { 1 , 5 , 10 , 4 , 7 }, x = 4 ;
int n = a.length;
System.out.println(Next_greater(a, n, x));
}
}
|
Python3
def Next_greater(a, n, x):
a = sorted (a)
low, high, ans = 0 , n - 1 , x + 1
while (low < = high):
mid = (low + high) / / 2
if (a[mid] < = ans):
if (a[mid] = = ans):
ans + = 1
high = n - 1
low = mid + 1
else :
high = mid - 1
return ans
a = [ 1 , 5 , 10 , 4 , 7 ]
x = 4
n = len (a)
print (Next_greater(a, n, x))
|
C#
using System;
class GFG
{
static int Next_greater( int []a, int n, int x)
{
Array.Sort(a);
int low = 0, high = n - 1, ans = x + 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (a[mid] <= ans)
{
if (a[mid] == ans)
{
ans++;
high = n - 1;
}
low = mid + 1;
}
else
high = mid - 1;
}
return ans;
}
public static void Main(String[] args)
{
int []a = { 1, 5, 10, 4, 7 };
int x = 4;
int n = a.Length;
Console.WriteLine(Next_greater(a, n, x));
}
}
|
PHP
<?php
function Next_greater( $a , $n , $x )
{
sort( $a );
$low = 0;
$high = $n - 1;
$ans = $x + 1;
while ( $low <= $high )
{
$mid = ( $low + $high ) / 2;
if ( $a [ $mid ] <= $ans )
{
if ( $a [ $mid ] == $ans )
{
$ans ++;
$high = $n - 1;
}
$low = $mid + 1;
}
else
$high = $mid - 1;
}
return $ans ;
}
$a = array ( 1, 5, 10, 4, 7 );
$x = 4;
$n = count ( $a );
echo Next_greater( $a , $n , $x );
?>
|
Javascript
<script>
function Next_greater( a, n, x){
a.sort( function (aa, bb){ return aa - bb});
let low = 0, high = n - 1, ans = x + 1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (a[mid] <= ans) {
if (a[mid] == ans) {
ans++;
high = n - 1;
}
low = mid + 1;
}
else
high = mid - 1;
}
return ans;
}
let a = [ 1, 5, 10, 4, 7 ]
let x = 4;
let n = a.length;
document.write( Next_greater(a, n, x));
</script>
|
Time complexity: O( n*log2n ) as sorting is required for implementation.
Auxiliary Space: O(1)