📌  相关文章
📜  从未排序的数组中找到最大的正整数 x,使得 min(arr[]) < x < max(arr[])

📅  最后修改于: 2022-05-13 01:56:04.767000             🧑  作者: Mango

从未排序的数组中找到最大的正整数 x,使得 min(arr[]) < x < max(arr[])

给定一个包含整数的数组arr[] 。任务是找到数组中缺失的最大正整数x ,使得min(arr[]) < x < max(arr[])

例子

朴素方法:对数组进行降序排序并返回第一个缺失的正数。如果没有丢失,则返回-1

时间复杂度: O(N logN)
时间复杂度: O(1)

有效的方法:这个问题可以通过使用散列来解决。构建给定数组中所有正元素的 Hashmap。建好Hashmap后,逆向查看HashMap,返回第一个缺失的正数。如果没有丢失,则返回-1

下面是上述方法的实现。

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to find missing positive integer
int firstMissingPositive(vector& nums)
{
    int n = nums.size();
 
    // Map to store the elements
    map m;
    for (int i = 0; i < n; i++) {
        if (m.find(nums[i]) == m.end()) {
            m.insert({ nums[i], 1 });
        }
    }
 
    int ans = 0;
 
    // Traversing the Hashmap from reverse
    for (ans = m.rbegin()->first; ans > 0; ans--) {
        if (m.find(ans) == m.end())
            break;
    }
    return ans;
}
 
// Driver code
int main()
{
    vector arr = { 2, 3, 7, 6, 8 };
    int missing = firstMissingPositive(arr) == 0
                      ? -1
                      : firstMissingPositive(arr);
    cout << missing;
    return 0;
}


Java
// Java program for above approach
import java.util.*;
class GFG
{
 
  // Function to find missing positive integer
  public static int firstMissingPositive(int[] nums)
  {
    int n = nums.length;
 
    // Map to store the elements
    HashMap m = new HashMap<>();
    for (int i = 0; i < n; i++) {
      if (m.containsKey(nums[i]) == false) {
        m.put(nums[i], 1);
      }
    }
    int ans = 0;
 
    for (Map.Entry temp :
         m.entrySet()) {
      ans = Math.max(ans, temp.getKey());
    }
 
    // Traversing the Hashmap from reverse
    for (; ans > 0; ans--) {
      if (m.containsKey(ans) == false)
        break;
    }
    return ans;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int[] arr = { 2, 3, 7, 6, 8 };
    if (firstMissingPositive(arr) == 0)
      System.out.print(-1);
    else
      System.out.print(firstMissingPositive(arr));
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python program for above approach
 
# Function to find missing positive integer
def firstMissingPositive(nums):
    n = len(nums)
 
    # Map to store the elements
    m = {}
    for i in range(n):
        if (nums[i] not in m):
            m[nums[i]] = 1
 
    ans = 0
    for itm in m.keys():
        ans = max(ans, itm)
     
    # Traversing the Hashmap from reverse
    while(ans >= 0):
        if (ans not in m):
            break
        ans -= 1
 
    return ans
 
# Driver code
arr = [2, 3, 7, 6, 8]
missing = -1 if firstMissingPositive(arr) == 0 else firstMissingPositive(arr)
print(missing)
 
# This code is contributed by shinjanpatra


C#
// C# program for above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
  // Function to find missing positive integer
  public static int firstMissingPositive(int[] nums)
  {
    int n = nums.Length;
 
    // Map to store the elements
    Dictionary m = new Dictionary();
    for (int i = 0; i < n; i++) {
      if (m.ContainsKey(nums[i]) == false) {
        m.Add(nums[i], 1);
      }
    }
    int ans = 0;
 
    foreach (KeyValuePair temp in
             m) {
      ans = Math.Max(ans, temp.Key);
    }
 
    // Traversing the Hashmap from reverse
    for (; ans > 0; ans--) {
      if (m.ContainsKey(ans) == false)
        break;
    }
    return ans;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int[] arr = { 2, 3, 7, 6, 8 };
    if (firstMissingPositive(arr) == 0)
      Console.Write(-1);
    else
      Console.Write(firstMissingPositive(arr));
  }
}
 
// This code is contributed by 29AjayKumar


Javascript



输出
5

时间复杂度: O(N)
辅助空间: O(N)