📌  相关文章
📜  最小化除法,使得任何 Array 元素都不能被 K 整除

📅  最后修改于: 2022-05-13 01:56:10.699000             🧑  作者: Mango

最小化除法,使得任何 Array 元素都不能被 K 整除

给定一个大小为N的数组arr [] 和一个整数K ,任务是找到最小操作,使得没有变量可以被 K 整除。在每个操作中:

  • 从数组中选择任何整数X。
  • 将所有出现的X除以K

例子:

方法:这个问题可以借助基于以下思想的贪心方法来解决。

请按照以下步骤解决问题:

  • 初始化一个空以存储执行操作时获得的所有不同元素。
  • 循环遍历数组arr[]的所有元素。
    • 运行一个 while 循环,直到当前arr[i]可以被K整除。
      • 如果集合中不存在arr[i] ,则插入它。
      • 将 arr[i]除以“K”。
  • 返回集合的大小,因为它表示执行除法运算的次数。

以下是上述方法的实现:

C++
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function to find
// the minimum number of steps required
int minimumOps(int n, int k, vector& arr)
{
    // Initializing an empty set 'elements'.
    set elements;
 
    // Looping over the array 'arr'.
    for (int i = 0; i < n; i++) {
        // While loop till current
        // element is divisible by
        // 'k'.
        while (arr[i] % k == 0) {
            // If current array element is
            // not in the set then insert it.
            if (elements.find(arr[i])
                == elements.end()) {
                elements.insert(arr[i]);
            }
            // Dividing the current
            // array element by 'k'.
            arr[i] /= k;
        }
    }
 
    // Returning the answer:
    // size of the set 'elements'.
    return (int)elements.size();
}
 
// Driver code
int main()
{
    int N = 5, K = 4;
    vector arr = { 3, 5, 8, 12, 4 };
 
    // Function call
    cout << minimumOps(n, k, arr);
    return 0;
}


Java
// Java code to implement the approach
import java.util.*;
 
class GFG {
 
  // Function to find
  // the minimum number of steps required
  static int minimumOps(int n, int k, int arr[])
  {
     
    // Initializing an empty set 'elements'.
    HashSet elements=new HashSet(); 
 
    // Looping over the array 'arr'.
    for (int i = 0; i < n; i++) {
      // While loop till current
      // element is divisible by
      // 'k'.
      while (arr[i] % k == 0) {
        // If current array element is
        // not in the set then insert it.
        if (!elements.contains(arr[i])) {
          elements.add(arr[i]);
        }
        // Dividing the current
        // array element by 'k'.
        arr[i] /= k;
      }
    }
 
    // Returning the answer:
    // size of the set 'elements'.
    return elements.size();
  }
 
  // Driver code
  public static void main (String[] args) {
    int N = 5, K = 4;
    int arr[] = { 3, 5, 8, 12, 4 };
 
    // Function call
    System.out.print(minimumOps(N, K, arr));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python3 code to implement the approach
 
# Function to find the minimum number of steps required
def minimumOps(n, k, arr):
   
    # Initializing an empty set 'elements'.
    elements = set()
     
    # Looping over the array 'arr'.
    for i in range(n):
       
        # While loop till current
        # element is divisible by
        # 'k'.
        while (arr[i] % k == 0):
           
            # If current array element is
            # not in the set then insert it.
            if arr[i] not in elements:
                elements.add(arr[i])
                 
            # Dividing the current
            # array element by 'k'.
            arr[i] //= k
             
    # Returning the answer:
    # size of the set 'elements'.
    return len(elements)
 
# Driver Code
N, K = 5, 4
arr = [3, 5, 8, 12, 4]
 
# Function Call
print(minimumOps(N, K, arr))
 
# This code is contributed by phasing17


C#
// C# code to implement the approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
  // Function to find
  // the minimum number of steps required
  static int minimumOps(int n, int k, int[] arr)
  {
 
    // Initializing an empty set 'elements'.
    HashSet elements = new HashSet();
 
    // Looping over the array 'arr'.
    for (int i = 0; i < n; i++)
    {
 
      // While loop till current
      // element is divisible by
      // 'k'.
      while (arr[i] % k == 0)
      {
 
        // If current array element is
        // not in the set then insert it.
        if (!(elements.Contains(arr[i]))) {
          elements.Add(arr[i]);
        }
        // Dividing the current
        // array element by 'k'.
        arr[i] /= k;
      }
    }
 
    // Returning the answer:
    // size of the set 'elements'.
    return elements.Count;
  }
 
  public static void Main(string[] args)
  {
    int N = 5, K = 4;
    int[] arr = { 3, 5, 8, 12, 4 };
 
    // Function call
    Console.Write(minimumOps(N, K, arr));
  }
}
 
// This code is contributed by phasing17.


Javascript


输出
3

时间复杂度: O(N * LogM),其中 M 是数组的最大元素。
辅助空间:O(N)