📜  最小数除以数组中的最小元素数套装2

📅  最后修改于: 2021-04-27 18:58:24             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是找到最小的数,该数将数组中元素的最小数量相除。

例子:

方法:让我们先观察一些细节。将零元素相除的数字已经存在,即max(arr)+ 1 。现在,我们只需要找到将数组中的零数相除的最小数即可。

在本文中,将讨论使用筛网(M = max(arr))在O(M * log(M)+ N)时间内解决此问题的方法。

  • 首先,在数组中找到最大元素M ,并创建一个长度为M +1的频率表freq [] ,以存储1M之间的数字的频率。
  • 迭代数组,并为每个索引i将freq []更新为freq [arr [i]] ++
  • 现在,应用筛网算法。在1M +1之间的所有元素之间进行迭代。
    • 假设我们要迭代数字X。
    • 创建一个临时变量cnt
    • 对于XM之间的每个X倍数{X,2X,3X…。},将cnt更新为cnt = cnt + freq [kX]
    • 如果CNT = 0,那么答案将是X否则继续迭代为X的下一个值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the smallest number
// that divides minimum number of elements 
// in the given array
int findMin(int* arr, int n)
{
    // m stores the maximum in the array
    int m = 0;
    for (int i = 0; i < n; i++)
        m = max(m, arr[i]);
  
    // Frequency array
    int freq[m + 2] = { 0 };
    for (int i = 0; i < n; i++)
        freq[arr[i]]++;
  
    // Sieve
    for (int i = 1; i <= m + 1; i++) {
        int j = i;
        int cnt = 0;
          
        // Incrementing j
        while (j <= m) {
            cnt += freq[j];
            j += i;
        }
  
        // If no multiples of j are
        // in the array
        if (!cnt)
            return i;
    }
  
    return m + 1;
}
  
// Driver code
int main()
{
    int arr[] = { 2, 12, 6 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << findMin(arr, n);
      
    return 0;
}


Java
// Java implementation of the approach 
class GFG
{
      
    // Function to return the smallest number 
    // that divides minimum number of elements 
    // in the given array 
    static int findMin(int arr[], int n) 
    { 
        // m stores the maximum in the array 
        int m = 0; 
        for (int i = 0; i < n; i++) 
            m = Math.max(m, arr[i]); 
      
        // Frequency array 
        int freq [] = new int[m + 2]; 
        for (int i = 0; i < n; i++) 
            freq[arr[i]]++; 
      
        // Sieve 
        for (int i = 1; i <= m + 1; i++)
        { 
            int j = i; 
            int cnt = 0; 
              
            // Incrementing j 
            while (j <= m) 
            { 
                cnt += freq[j]; 
                j += i; 
            } 
      
            // If no multiples of j are 
            // in the array 
            if (cnt == 0) 
                return i; 
        } 
        return m + 1; 
    } 
      
    // Driver code 
    public static void main (String[] args) 
    { 
        int arr[] = { 2, 12, 6 }; 
        int n = arr.length; 
      
        System.out.println(findMin(arr, n)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
  
# Function to return the smallest number
# that divides minimum number of elements
# in the given array
def findMin(arr, n):
      
    # m stores the maximum in the array
    m = 0
    for i in range(n):
        m = max(m, arr[i])
  
    # Frequency array
    freq = [0] * (m + 2)
    for i in range(n):
        freq[arr[i]] += 1
  
    # Sieve
    for i in range(1, m + 2):
        j = i
        cnt = 0
  
        # Incrementing j
        while (j <= m):
            cnt += freq[j]
            j += i
  
        # If no multiples of j are
        # in the array
        if (not cnt):
            return i
  
    return m + 1
  
# Driver code
arr = [2, 12, 6]
n = len(arr)
  
print(findMin(arr, n))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
      
    // Function to return the smallest number 
    // that divides minimum number of elements 
    // in the given array 
    static int findMin(int []arr, int n) 
    { 
        // m stores the maximum in the array 
        int m = 0; 
        for (int i = 0; i < n; i++) 
            m = Math.Max(m, arr[i]); 
      
        // Frequency array 
        int []freq = new int[m + 2]; 
        for (int i = 0; i < n; i++) 
            freq[arr[i]]++; 
      
        // Sieve 
        for (int i = 1; i <= m + 1; i++) 
        { 
            int j = i; 
            int cnt = 0; 
              
            // Incrementing j 
            while (j <= m) 
            { 
                cnt += freq[j]; 
                j += i; 
            } 
      
            // If no multiples of j are 
            // in the array 
            if (cnt == 0) 
                return i; 
        } 
        return m + 1; 
    } 
      
    // Driver code 
    public static void Main () 
    { 
        int []arr = { 2, 12, 6 }; 
        int n = arr.Length; 
      
        Console.WriteLine(findMin(arr, n)); 
    } 
} 
  
// This code is contributed by AnkitRai01


输出 :

5

时间复杂度: O(Mlog(M)+ N)