Minimize divisions such that no Array element is divisible by K
Last Updated :
08 Apr, 2022
Given an array arr[] of size N and an integer K, the task is to find the minimum operations such that no variable is divisible by K. In each operation:
- Select any integer X from the array.
- Divide all occurrences of X by K.
Examples:
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].
Approach: This problem can be solved with the help of greedy approach based on the following idea.
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
Follow the steps mentioned below to solve the problem:
- Initialize an empty set to store all the different elements obtained while performing the operations.
- Loop over all elements of the array arr[].
- Run a while loop till current arr[i] is divisible by K.
- If arr[i] is not present in the set then insert it.
- Divide arr[i] by ‘K’.
- Return the size of the set as it represents the number of times division operation is performed.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int minimumOps( int n, int k, vector< int >& arr)
{
set< int > elements;
for ( int i = 0; i < n; i++) {
while (arr[i] % k == 0) {
if (elements.find(arr[i])
== elements.end()) {
elements.insert(arr[i]);
}
arr[i] /= k;
}
}
return ( int )elements.size();
}
int main()
{
int N = 5, K = 4;
vector< int > arr = { 3, 5, 8, 12, 4 };
cout << minimumOps(n, k, arr);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int minimumOps( int n, int k, int arr[])
{
HashSet<Integer> elements= new HashSet<Integer>();
for ( int i = 0 ; i < n; i++) {
while (arr[i] % k == 0 ) {
if (!elements.contains(arr[i])) {
elements.add(arr[i]);
}
arr[i] /= k;
}
}
return elements.size();
}
public static void main (String[] args) {
int N = 5 , K = 4 ;
int arr[] = { 3 , 5 , 8 , 12 , 4 };
System.out.print(minimumOps(N, K, arr));
}
}
|
Python3
def minimumOps(n, k, arr):
elements = set ()
for i in range (n):
while (arr[i] % k = = 0 ):
if arr[i] not in elements:
elements.add(arr[i])
arr[i] / / = k
return len (elements)
N, K = 5 , 4
arr = [ 3 , 5 , 8 , 12 , 4 ]
print (minimumOps(N, K, arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int minimumOps( int n, int k, int [] arr)
{
HashSet< int > elements = new HashSet< int >();
for ( int i = 0; i < n; i++)
{
while (arr[i] % k == 0)
{
if (!(elements.Contains(arr[i]))) {
elements.Add(arr[i]);
}
arr[i] /= k;
}
}
return elements.Count;
}
public static void Main( string [] args)
{
int N = 5, K = 4;
int [] arr = { 3, 5, 8, 12, 4 };
Console.Write(minimumOps(N, K, arr));
}
}
|
Javascript
<script>
const minimumOps = (n, k, arr) => {
let elements = new Set();
for (let i = 0; i < n; i++)
{
while (arr[i] % k == 0)
{
if (!elements.has(arr[i]))
{
elements.add(arr[i]);
}
arr[i] = parseInt(arr[i] / k);
}
}
return elements.size;
}
let n = 5, k = 4;
let arr = [3, 5, 8, 12, 4];
document.write(minimumOps(n, k, arr));
</script>
|
Time Complexity: O(N * LogM), where M is the maximum element of the array.
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...