📜  数组中丢失的最小素数

📅  最后修改于: 2021-04-29 03:29:32             🧑  作者: Mango

给定一个包含n个不同数字的数组。任务是找到阵列中不存在的最小素数。

注意:如果在数组的最大元素前没有素数,请打印“不缺少素数”。

例子:

Input: arr[] = {9, 11, 4, 2, 3, 7, 0, 1}
Output: 5
5 is the smallest prime, which is not present in array.

Input: arr[] = {3, 0, 2, 5}
Output: No prime number missing
As 5 is the maximum element and all prime numbers upto 5 
are present in the array.

方法:首先,使用Eratosthenes筛子查找所有素数,然后顺序检查那里不存在哪个素数。只需遍历数组并使用哈希检查数字是否存在。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
#define ll long long int
  
// this store all prime number
// upto 10^5
// this function find all prime
vector findPrime(int MAX)
{
    bool pm[MAX + 1];
    memset(pm, true, sizeof(pm));
  
    // use sieve to find prime
    pm[0] = pm[1] = false;
    for (int i = 2; i <= MAX; i++)
        if (pm[i])
            for (int j = 2 * i; j <= MAX; j += i)
                pm[j] = false;
  
    // if number is prime then
    // store it in prime vector
    vector prime;
    for (int i = 0; i <= MAX; i++)
        if (pm[i])
            prime.push_back(i);
  
    return prime;
}
  
// Function to find the smallest prime missing
int findSmallest(int arr[], int n)
{
    int MAX = *max_element(arr, arr + n);
  
    // first of all find all prime
    vector prime = findPrime(MAX);
  
    // now store all number as index of freq arr
    // so that we can improve searching
    unordered_set s;
    for (int i = 0; i < n; i++)
        s.insert(arr[i]);
  
    // now check for each prime
    int ans = -1;
    for (int i = 0; i < prime.size(); i++)
        if (s.find(prime[i]) == s.end()) {
            ans = prime[i];
            break;
        }
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 0, 1, 2, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // find smallest prime
    // which is not present
    if (findSmallest(arr, n) == -1)
        cout << "No prime number missing";
    else
        cout << findSmallest(arr, n);
  
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
  
class GFG {
  
    // this store all prime number
    // upto 10^5
    // this function find all prime
    static Vector findPrime(int MAX)
    {
        boolean pm[] = new boolean[MAX + 1];
        for (int i = 0; i < pm.length; i++)
            pm[i] = true;
  
        // use sieve to find prime
        pm[0] = pm[1] = false;
        for (int i = 2; i <= MAX; i++)
            if (pm[i])
                for (int j = 2 * i; j <= MAX; j += i)
                    pm[j] = false;
  
        // if number is prime then
        // store it in prime vector
        Vector prime = new Vector();
        for (int i = 0; i <= MAX; i++)
            if (pm[i])
                prime.add(i);
  
        return prime;
    }
  
    static int max_element(int arr[])
    {
        int max = arr[0];
  
        for (int i = 0; i < arr.length; i++)
            max = Math.max(max, arr[i]);
  
        return max;
    }
  
    // Function to find the smallest prime missing
    static int findSmallest(int arr[], int n)
    {
        int MAX = max_element(arr);
  
        // first of all find all prime
        Vector prime = findPrime(MAX);
  
        // now store all number as index of freq arr
        // so that we can improve searching
        Set s = new HashSet();
        for (int i = 0; i < arr.length; i++)
            s.add(arr[i]);
  
        // now check for each prime
        long ans = -1;
        for (int i = 0; i < prime.size(); i++) {
            if (!s.contains(prime.get(i))) {
  
                ans = (prime.get(i));
                break;
            }
        }
        return (int)ans;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 3, 0, 1, 2, 7 };
        int n = arr.length;
  
        // find smallest prime
        // which is not present
        if (findSmallest(arr, n) == -1)
            System.out.print("No prime number missing");
        else
            System.out.print(findSmallest(arr, n));
    }
}
  
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the above approach 
  
# This function finds all 
# prime numbers upto 10 ^ 5
def findPrime(MAX): 
  
    pm = [True] * (MAX + 1) 
  
    # use sieve to find prime 
    pm[0], pm[1] = False, False
      
    for i in range(2, MAX + 1): 
        if pm[i] == True:
              
            for j in range(2 * i, MAX + 1, i): 
                pm[j] = False
  
    # If number is prime, then 
    # store it in prime list 
    prime = []
    for i in range(0, MAX + 1): 
        if pm[i] == True: 
            prime.append(i) 
  
    return prime 
  
# Function to find the smallest prime missing 
def findSmallest(arr, n): 
  
    MAX = max(arr)
      
    # first of all find all prime 
    prime = findPrime(MAX) 
  
    # now store all number as index of freq 
    # arr so that we can improve searching 
    s = set() 
    for i in range(0, n): 
        s.add(arr[i]) 
  
    # now check for each prime 
    ans = -1
    for i in range(0, len(prime)): 
        if prime[i] not in s: 
            ans = prime[i] 
            break
          
    return ans 
  
# Driver Code
if __name__ == "__main__":
  
    arr = [3, 0, 1, 2,  7] 
    n = len(arr) 
  
    # find smallest prime 
    # which is not present 
    if(findSmallest(arr, n) == -1):
        print("No prime number missing")
    else:
        print(findSmallest(arr, n))
  
# This code is contributed by Rituraj Jain


C#
// C# implementation of the above approach
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG 
{
  
    // this store all prime number
    // upto 10^5
    // this function find all prime
    static ArrayList findPrime(int MAX)
    {
        bool[] pm = new bool[MAX + 1];
        for (int i = 0; i < MAX + 1; i++)
            pm[i] = true;
  
        // use sieve to find prime
        pm[0] = pm[1] = false;
        for (int i = 2; i <= MAX; i++)
            if (pm[i])
                for (int j = 2 * i; j <= MAX; j += i)
                    pm[j] = false;
  
        // if number is prime then
        // store it in prime vector
        ArrayList prime = new ArrayList();
        for (int i = 0; i <= MAX; i++)
            if (pm[i])
                prime.Add(i);
  
        return prime;
    }
  
    static int max_element(int []arr)
    {
        int max = arr[0];
  
        for (int i = 0; i < arr.Length; i++)
            max = Math.Max(max, arr[i]);
  
        return max;
    }
  
    // Function to find the smallest prime missing
    static int findSmallest(int []arr, int n)
    {
        int MAX = max_element(arr);
  
        // first of all find all prime
        ArrayList prime = findPrime(MAX);
  
        // now store all number as index of freq arr
        // so that we can improve searching
        HashSet s = new HashSet();
        for (int i = 0; i < arr.Length; i++)
            s.Add(arr[i]);
  
        // now check for each prime
        int ans = -1;
        for (int i = 0; i < prime.Count; i++) 
        {
            if (!s.Contains((int)prime[i])) 
            {
  
                ans = (int)(prime[i]);
                break;
            }
        }
        return (int)ans;
    }
  
    // Driver code
    static void Main()
    {
        int []arr = { 3, 0, 1, 2, 7 };
        int n = arr.Length;
  
        // find smallest prime
        // which is not present
        if (findSmallest(arr, n) == -1)
            Console.Write("No prime number missing");
        else
            Console.Write(findSmallest(arr, n));
    }
}
  
// This code is contributed by mits


输出:
5