Open In App

Smallest element greater than X not present in the array

Last Updated : 25 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 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++
// 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;
}
Java
// 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 
Python3
# 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
C#
// 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
PHP
<?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. 
?> 
JavaScript
<script>

// Js 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
    a.sort(function(aa, bb){return aa - bb});

    let low = 0, high = n - 1, ans = x + 1;

    // Continue until low is less
    // than or equals to high
    while (low <= high) {

        // Find mid
        let mid = Math.floor((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
let a = [ 1, 5, 10, 4, 7 ]
let x = 4;
let n = a.length;

document.write( Next_greater(a, n, x));
</script>

Output: 
6

 

Time complexity: O( n*log2n ) as sorting is required for implementation.
Auxiliary Space: O(1)


Next Article
Article Tags :

Similar Reads