📌  相关文章
📜  查找未排序数组中缺少的最小正数|套装3

📅  最后修改于: 2021-04-22 00:01:46             🧑  作者: Mango

给定一个未排序的数组,其中同时包含正和负元素。任务是找到数组中缺少的最小正数。

例子:

方法 :
我们已经讨论了一些从未排序的数组中寻找最小正数的技术。

查找未排序数组中缺少的最小正数|套装1
查找未排序数组中缺少的最小正数|套装2

在这里,我们使用set存储所有正整数,并找到第一个缺失的正整数。

C++
// CPP program to find the smallest
// positive missing number
#include 
using namespace std;
  
// Function to find the smallest
// positive missing number
int findMissingPositive(int arr[], int n)
{
    // Default smallest Positive Integer
    int m = 1;
  
    // Store values in set which are
    // greater than variable m
    set x;
  
    for (int i = 0; i < n; i++) {
        // Store value when m is less than
        // current index of given array
        if (m < arr[i]) {
            x.insert(arr[i]);
        }
        else if (m == arr[i]) {
            // Increment m when it is equal
            // to current element
            m = m + 1;
  
            while (x.count(m)) {
                x.erase(m);
  
                // Increment m when it is one of the
                // element of the set
                m = m + 1;
            }
        }
    }
  
    // Return the required answer
    return m;
}
  
// Driver code
int main()
{
    int arr[] = { 2, 3, -7, 6, 8, 1, -10, 15 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function call
    cout << findMissingPositive(arr, n);
  
    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 findMissingPositive(int arr[], int n)
{
    // Default smallest Positive Integer
    int m = 1;
  
    // Store values in set which are
    // greater than variable m
    HashSet x = new HashSet();
  
    for (int i = 0; i < n; i++)
    {
        // Store value when m is less than
        // current index of given array
        if (m < arr[i])
        {
            x.add(arr[i]);
        }
        else if (m == arr[i])
        {
            // Increment m when it is equal
            // to current element
            m = m + 1;
  
            while (x.contains(m)) 
            {
                x.remove(m);
  
                // Increment m when it is one of the
                // element of the set
                m = m + 1;
            }
        }
    }
  
    // Return the required answer
    return m;
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 2, 3, -7, 6, 8, 1, -10, 15 };
  
    int n = arr.length;
  
    // Function call
    System.out.println(findMissingPositive(arr, n));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python 3 program to find the smallest
# positive missing number
  
# Function to find the smallest
# positive missing number
def findMissingPositive(arr, n):
      
    # Default smallest Positive Integer
    m = 1
  
    # Store values in set which are
    # greater than variable m
    x = []
    for i in range(n):
          
        # Store value when m is less than
        # current index of given array
        if (m < arr[i]):
            x.append(arr[i])
      
        elif (m == arr[i]):
              
            # Increment m when it is equal
            # to current element
            m = m + 1
  
            while (x.count(m)):
                x.remove(m)
  
                # Increment m when it is one of the
                # element of the set
                m = m + 1
  
    # Return the required answer
    return m
  
# Driver code
if __name__ == '__main__':
    arr = [2, 3, -7, 6, 8, 1, -10, 15]
  
    n = len(arr)
  
    # Function call
    print(findMissingPositive(arr, n))
  
# This code is contributed by
# Surendra_Gangwar


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 findMissingPositive(int []arr, int n)
{
    // Default smallest Positive Integer
    int m = 1;
  
    // Store values in set which are
    // greater than variable m
    HashSet x = new HashSet();
  
    for (int i = 0; i < n; i++)
    {
        // Store value when m is less than
        // current index of given array
        if (m < arr[i])
        {
            x.Add(arr[i]);
        }
          
        else if (m == arr[i])
        {
            // Increment m when it is equal
            // to current element
            m = m + 1;
  
            while (x.Contains(m)) 
            {
                x.Remove(m);
  
                // Increment m when it is one of the
                // element of the set
                m = m + 1;
            }
        }
    }
  
    // Return the required answer
    return m;
}
  
// Driver code
public static void Main(String[] args) 
{
    int []arr = { 2, 3, -7, 6, 8, 1, -10, 15 };
  
    int n = arr.Length;
  
    // Function call
    Console.WriteLine(findMissingPositive(arr, n));
}
}
  
// This code is contributed by Rajput-Ji


输出:
4