📌  相关文章
📜  查找未排序数组中缺少的最小正数:哈希实现

📅  最后修改于: 2021-10-28 02:07:15             🧑  作者: Mango

给定一个包含 0 的正负元素的未排序数组。任务是在 O(N) 时间内从数组中找到最小的正数。
例子:

Input: arr[] = {-5, 2, 0, -1, -10, 15}
Output: 1

Input: arr[] = {0, 1, 2, 3, 4, 5}
Output: 6

Input: arr[] = {1, 1, 1, 0, -1, -2}
Output: 2

我们可以使用哈希来解决这个问题。这个想法是建立一个给定数组中所有正元素的哈希表。一旦构建了哈希表。我们可以在哈希表中查找从 1 开始的所有正整数。一旦我们找到哈希表中不存在的数字,我们就返回它。
我们可以使用 C++ 中的 unordered_map 来实现散列,它允许以几乎 O(1) 的时间复杂度执行查找操作。
下面是上述方法的实现:

C++
// C++ program to find the smallest
// positive missing number
 
#include 
using namespace std;
 
// Function to find the smallest positive
// missing number
int missingNumber(int a[], int n)
{
    // Declaring an unordered_map
    unordered_map mp;
 
    // if array value is positive
    // store it in map
    for (int i = 0; i < n; i++) {
        if (a[i] > 0)
            mp[a[i]]++;
    }
 
    // index value set to 1
    int index = 1;
 
    // Return the first value starting
    // from 1 which does not exists in map
    while (1) {
        if (mp.find(index) == mp.end()) {
            return index;
        }
 
        index++;
    }
}
 
// Driver code
int main()
{
    int a[] = { 1, 1, 1, 0, -1, -2 };
    int size = sizeof(a) / sizeof(a[0]);
 
    cout << "Smallest positive missing number is : "
         << missingNumber(a, size) << endl;
 
    return 0;
}


Java
// Java program to find the smallest
// positive missing number
 
import java.util.*;
 
class GFG
{
 
    // Function to find the smallest positive
    // missing number
    static int missingNumber(int a[], int n)
    {
        // Declaring an unordered_map
        Map mp = new LinkedHashMap<>();
 
        // if array value is positive
        // store it in map
        for (int i = 0; i < n; i++)
        {
            if (a[i] > 0)
            {
                mp.put(a[i], mp.get(a[i]) == null ? 1 : mp.get(a[i]) + 1);
            }
        }
 
        // index value set to 1
        int index = 1;
 
        // Return the first value starting
        // from 1 which does not exists in map
        while (true)
        {
            if (!mp.containsKey(index))
            {
                return index;
            }
 
            index++;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int a[] = {1, 1, 1, 0, -1, -2};
        int size = a.length;
 
        System.out.println("Smallest positive missing number is : "
                + missingNumber(a, size));
    }
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python3 program to find the smallest
# positive missing number
 
# Function to find the smallest positive
# missing number
def missingNumber(a, n) :
     
    # Declaring an unordered_map
    mp = dict();
 
    # if array value is positive
    # store it in map
    for i in range(n) :
        if (a[i] > 0) :
            if a[i] not in mp.keys() :
                mp[a[i]] = 0
           
            mp[a[i]] += 1
 
    # index value set to 1
    index = 1;
 
    # Return the first value starting
    # from 1 which does not exists in map
    while (1) :
        if (index not in mp.keys()) :
            return index;
         
 
        index += 1;
 
 
# Driver code
if __name__ == "__main__" :
 
    a = [ 1, 1, 1, 0, -1, -2 ];
    size = len(a);
 
    print("Smallest positive missing number is : ",missingNumber(a, size));
 
# This code is contributed by AnkitRai01


C#
// C# program to find the smallest
// positive missing number
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to find the smallest positive
    // missing number
    static int missingNumber(int []a, int n)
    {
        // Declaring an unordered_map
        Dictionary m = new Dictionary();
 
        // if array value is positive
        // store it in map
        for (int i = 0; i < n; i++)
        {
            if (a[i] > 0)
            {
                if(m.ContainsKey(a[i]))
                {
                    var val = m[a[i]];
                    m.Remove(a[i]);
                    m.Add(a[i], val + 1);
                }
                else
                {
                    m.Add(a[i], 1);
                }
            }
        }
 
        // index value set to 1
        int index = 1;
 
        // Return the first value starting
        // from 1 which does not exists in map
        while (true)
        {
            if (!m.ContainsKey(index))
            {
                return index;
            }
 
            index++;
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []a = {1, 1, 1, 0, -1, -2};
        int size = a.Length;
 
        Console.WriteLine("Smallest positive missing number is : "
                + missingNumber(a, size));
    }
}
 
// This code is contributed by Princi Singh


Javascript


输出:
Smallest positive missing number is : 2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程