📌  相关文章
📜  将数组减少到 0 元素所需的最小给定操作数

📅  最后修改于: 2021-10-26 06:54:20             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] 。任务是找到将数组减少到 0 个元素所需的最小给定操作数。在单个操作中,可以从数组中选择任何元素,并删除其所有倍数,包括其自身。
例子:

幼稚的方法:在每一步从数组中找到最小值并遍历整个数组以找到这些元素的倍数并删除它们。
有效的方法:

  • 创建一个计数数组,用于存储数组中每个数字的计数。
  • 由于我们知道对于数字 x,满足条件 (A % x == 0) 的元素实际上是 x 的倍数,因此我们需要找到每个数字的倍数并将它们的频率设置为 0,包括所选元素本身.
  • 现在对于每个数字,我们遍历它的倍数一次并从它的所有倍数中减去该数字的计数值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum
// operations required
int minOperations(int* arr, int n)
{
    int maxi, result = 0;
 
    // Count the frequency of each element
    vector freq(1000001, 0);
    for (int i = 0; i < n; i++) {
        int x = arr[i];
        freq[x]++;
    }
 
    // Maximum element from the array
    maxi = *(max_element(arr, arr + n));
    for (int i = 1; i <= maxi; i++) {
        if (freq[i] != 0) {
 
            // Find all the multiples of i
            for (int j = i * 2; j <= maxi; j = j + i) {
 
                // Delete the multiples
                freq[j] = 0;
            }
 
            // Increment the operations
            result++;
        }
    }
    return result;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 4, 2, 4, 4, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << minOperations(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.Arrays;
 
class GFG
{
 
    // Function to return the minimum
    // operations required
    static int minOperations(int[] arr, int n)
    {
        int maxi, result = 0;
 
        // Count the frequency of each element
        int[] freq = new int[1000001];
        for (int i = 0; i < n; i++)
        {
            int x = arr[i];
            freq[x]++;
        }
 
        // Maximum element from the array
        maxi = Arrays.stream(arr).max().getAsInt();
        for (int i = 1; i <= maxi; i++)
        {
            if (freq[i] != 0)
            {
 
                // Find all the multiples of i
                for (int j = i * 2; j <= maxi; j = j + i)
                {
 
                    // Delete the multiples
                    freq[j] = 0;
                }
 
                // Increment the operations
                result++;
            }
        }
        return result;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = {2, 4, 2, 4, 4, 4};
        int n = arr.length;
 
        System.out.println(minOperations(arr, n));
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
 
# Function to return the minimum
# operations required
def minOperations(arr, n):
 
    result = 0
     
    # Count the frequency of each element
    freq = [0] * 1000001
     
    for i in range(0, n):
        freq[arr[i]] += 1
 
    # Maximum element from the array
    maxi = max(arr)
    for i in range(1, maxi+1):
        if freq[i] != 0:
 
            # Find all the multiples of i
            for j in range(i * 2, maxi+1, i):
 
                # Delete the multiples
                freq[j] = 0
 
            # Increment the operations
            result += 1
         
    return result
 
# Driver code
if __name__ == "__main__":
 
    arr = [2, 4, 2, 4, 4, 4]
    n = len(arr)
 
    print(minOperations(arr, n))
 
# This code is contributed by Rituraj Jain


C#
// C# implementation of above approach
using System;
using System.Linq;
 
class GFG
{
 
    // Function to return the minimum
    // operations required
    static int minOperations(int[] arr, int n)
    {
        int maxi, result = 0;
 
        // Count the frequency of each element
        int[] freq = new int[1000001];
        for (int i = 0; i < n; i++)
        {
            int x = arr[i];
            freq[x]++;
        }
 
        // Maximum element from the array
        maxi = arr.Max();
        for (int i = 1; i <= maxi; i++)
        {
            if (freq[i] != 0)
            {
 
                // Find all the multiples of i
                for (int j = i * 2; j <= maxi; j = j + i)
                {
 
                    // Delete the multiples
                    freq[j] = 0;
                }
 
                // Increment the operations
                result++;
            }
        }
        return result;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = {2, 4, 2, 4, 4, 4};
        int n = arr.Length;
 
        Console.WriteLine(minOperations(arr, n));
    }
}
 
// This code is contributed by Rajput-Ji


PHP


Javascript


输出:
1

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