Smallest element greater than X not present in the array

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 samllest 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 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the smallest element greater
// than x which is not present in a[]
int Next_greater(int a[], int n, int x)
{
  
    // Sort the array
    sort(a, a + n);
  
    int low = 0, high = n - 1, ans = x + 1;
  
    // Continue until low is less
    // than or equals to high
    while (low <= high) {
  
        // Find mid
        int mid = (low + high) / 2;
  
        // If element at mid is less than
        // or equals to searching element
        if (a[mid] <= ans) {
  
            // If mid is equals
            // to searching element
            if (a[mid] == ans) {
  
                // Increment searching element
                ans++;
  
                // Make high as N - 1
                high = n - 1;
            }
  
            // Make low as mid + 1
            low = mid + 1;
        }
  
        // Make high as mid - 1
        else
            high = mid - 1;
    }
  
    // Return the next greater element
    return ans;
}
  
// Driver code
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function to return the smallest element greater
// than x which is not present in a[]
static int Next_greater(int a[], int n, int x)
{
  
    // Sort the array
    Arrays.sort(a);
  
    int low = 0, high = n - 1, ans = x + 1;
  
    // Continue until low is less
    // than or equals to high
    while (low <= high) 
    {
  
        // Find mid
        int mid = (low + high) / 2;
  
        // If element at mid is less than
        // or equals to searching element
        if (a[mid] <= ans) 
        {
  
            // If mid is equals
            // to searching element
            if (a[mid] == ans) 
            {
  
                // Increment searching element
                ans++;
  
                // Make high as N - 1
                high = n - 1;
            }
  
            // Make low as mid + 1
            low = mid + 1;
        }
  
        // Make high as mid - 1
        else
            high = mid - 1;
    }
  
    // Return the next greater element
    return ans;
}
  
// Driver code
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));
}
}
  
// This code is contributed by PrinciRaj1992 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the smallest element 
# greater than x which is not present in a[]
def Next_greater(a, n, x):
  
    # Sort the array
    a = sorted(a)
  
    low, high, ans = 0, n - 1, x + 1
  
    # Continue until low is less
    # than or equals to high
    while (low <= high):
  
        # Find mid
        mid = (low + high) // 2
  
        # If element at mid is less than
        # or equals to searching element
        if (a[mid] <= ans):
  
            # If mid is equals
            # to searching element
            if (a[mid] == ans):
  
                # Increment searching element
                ans += 1
  
                # Make high as N - 1
                high = n - 1
  
            # Make low as mid + 1
            low = mid + 1
  
        # Make high as mid - 1
        else:
            high = mid - 1
  
    # Return the next greater element
    return ans
  
# Driver code
a = [1, 5, 10, 4, 7]
x = 4
n = len(a)
  
print(Next_greater(a, n, x))
  
# This code is contributed 
# by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
      
class GFG 
{
  
// Function to return the smallest element greater
// than x which is not present in a[]
static int Next_greater(int []a, int n, int x)
{
  
    // Sort the array
    Array.Sort(a);
  
    int low = 0, high = n - 1, ans = x + 1;
  
    // Continue until low is less
    // than or equals to high
    while (low <= high) 
    {
  
        // Find mid
        int mid = (low + high) / 2;
  
        // If element at mid is less than
        // or equals to searching element
        if (a[mid] <= ans) 
        {
  
            // If mid is equals
            // to searching element
            if (a[mid] == ans) 
            {
  
                // Increment searching element
                ans++;
  
                // Make high as N - 1
                high = n - 1;
            }
  
            // Make low as mid + 1
            low = mid + 1;
        }
  
        // Make high as mid - 1
        else
            high = mid - 1;
    }
  
    // Return the next greater element
    return ans;
}
  
// Driver code
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));
}
  
// This code is contributed by Princi Singh

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to return the smallest element greater
// than x which is not present in a[]
function Next_greater($a, $n, $x)
{
  
    // Sort the array
    sort($a);
  
    $low = 0;
    $high = $n - 1;
    $ans = $x + 1;
  
    // Continue until low is less
    // than or equals to high
    while ($low <= $high
    {
  
        // Find mid
        $mid = ($low + $high) / 2;
  
        // If element at mid is less than
        // or equals to searching element
        if ($a[$mid] <= $ans
        {
  
            // If mid is equals
            // to searching element
            if ($a[$mid] == $ans)
            {
  
                // Increment searching element
                $ans++;
  
                // Make high as N - 1
                $high = $n - 1;
            }
  
            // Make low as mid + 1
            $low = $mid + 1;
        }
  
        // Make high as mid - 1
        else
            $high = $mid - 1;
    }
  
    // Return the next greater element
    return $ans;
}
  
// Driver code
$a = array( 1, 5, 10, 4, 7 );
$x = 4;
$n = count($a);
  
echo Next_greater($a, $n, $x);
  
// This code is contributed by Naman_garg. 
?> 

chevron_right


Output:

6


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.