📌  相关文章
📜  数组中不存在大于X的最小元素

📅  最后修改于: 2021-04-21 22:06:43             🧑  作者: Mango

给定大小为N的数组arr []和整数X。任务是找到数组中不存在的大于X的最小元素。

例子:

方法:一种有效的解决方案基于二进制搜索。首先对数组进行排序。为零,高取为N – 1 。并搜索元素X +1。如果mid的元素( (low + high)/ 2)小于或等于search元素,则将low设为mid +1 。否则,取中– 1 。如果在中间的元素获得等于搜索元素,然后通过一个递增的搜索元素,使N – 1。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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


输出:
6