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++
// 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 = 4n = 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. ?> |
6
Recommended Posts:
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Smallest Greater Element on Right Side
- Smallest greater elements in whole array
- Find index of an extra element present in one sorted array
- Closest greater element for every array element from another array
- Find closest greater value for every element in array
- Elements greater than the previous and next element in an Array
- Kth smallest element in the array using constant space when array can't be modified
- Adding elements of an array until every element becomes greater than or equal to k
- Sum of elements in an array with frequencies greater than or equal to that element
- First strictly greater element in a sorted array in Java
- Closest greater or same value on left side for every element in array
- Rearrange an array such that every odd indexed element is greater than it previous
- Find element in a sorted array whose frequency is greater than or equal to n/2.
- Count the number of elements which are greater than any of element on right side of an array
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.



