最小化除法,使得任何 Array 元素都不能被 K 整除
给定一个大小为N的数组arr [] 和一个整数K ,任务是找到最小操作,使得没有变量可以被 K 整除。在每个操作中:
- 从数组中选择任何整数X。
- 将所有出现的X除以K 。
例子:
Input: arr[] = [2, 3, 4, 5], K = 2
Output: 2
Explanation:
In the first operation, choose X = 4; Hence the resulting array will be [2, 3, 2, 5].
In the second operation, choose X = 2; Hence the resulting array will be [1, 3, 1, 5].
After these two operations, no X remained such that X % K = 0; hence, the answer is 2.
Input: arr[] = [3, 5, 8, 12, 4], K = 4
Output: 3
Explanation:
First operation X = 12, arr[] = [3, 5, 8, 3, 4].
Second operation X = 8, arr[] = [3, 5, 2, 3, 4].
Third operation X = 4, arr[] = [3, 5, 2, 3, 1].
方法:这个问题可以借助基于以下思想的贪心方法来解决。
Select any element and repeatedly divide it by K until it is no more divisible. Do the same for all array elements and find the total count
请按照以下步骤解决问题:
- 初始化一个空集以存储执行操作时获得的所有不同元素。
- 循环遍历数组arr[]的所有元素。
- 运行一个 while 循环,直到当前arr[i]可以被K整除。
- 如果集合中不存在arr[i] ,则插入它。
- 将 arr[i]除以“K”。
- 运行一个 while 循环,直到当前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)