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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find
// the minimum number of steps required
int minimumOps(int n, int k, vector<int>& arr)
{
// Initializing an empty set 'elements'.
set<int> 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<int> 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<Integer> elements=new HashSet<Integer>();
// 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<int> elements = new HashSet<int>();
// 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
<script>
// JavaScript code to implement the approach
// Function to find
// the minimum number of steps required
const minimumOps = (n, k, arr) => {
// Initializing an empty set 'elements'.
let elements = new Set();
// Looping over the array 'arr'.
for (let 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.has(arr[i]))
{
elements.add(arr[i]);
}
// Dividing the current
// array element by 'k'.
arr[i] = parseInt(arr[i] / k);
}
}
// Returning the answer:
// size of the set 'elements'.
return elements.size;
}
// Driver code
let n = 5, k = 4;
let arr = [3, 5, 8, 12, 4];
// Function call
document.write(minimumOps(n, k, arr));
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N * LogM), where M is the maximum element of the array.
Auxiliary Space: O(N)
Similar Reads
Minimum K such that sum of array elements after division by K does not exceed S Given an array arr[] of N elements and an integer S. The task is to find the minimum number K such that the sum of the array elements does not exceed S after dividing all the elements by K. Note: Consider integer division.Examples: Input: arr[] = {10, 7, 8, 10, 12, 19}, S = 27 Output: 3 After dividi
11 min read
Maximum sum of elements divisible by K from the given array Given an array of integers and a number K. The task is to find the maximum sum which is divisible by K from the given array.Examples: Input: arr[] = {3, 6, 5, 1, 8}, k = 3 Output: 18 Explanation: 18 is formed by the elements 3, 6, 1, 8.Input: arr = { 43, 1, 17, 26, 15 } , k = 16 Output: 32 Explanati
15 min read
Minimize the maximum element in constructed Array with sum divisible by K Given two integers N and K, the task is to find the smallest value for maximum element of an array of size N consisting of positive integers whose sum of elements is divisible by K. Examples: Input: N = 4, K = 3Output: 2Explanation: Let the array be [2, 2, 1, 1]. Here, sum of elements of this array
4 min read
Find an array element such that all elements are divisible by it Given an array of numbers, find the number among them such that all numbers are divisible by it. If not possible print -1. Examples: Input : arr = {25, 20, 5, 10, 100} Output : 5 Explanation : 5 is an array element which divides all numbers. Input : arr = {9, 3, 6, 2, 15} Output : -1 Explanation : N
8 min read
Minimize count of divisions by D to obtain at least K equal array elements Given an array A[ ] of size N and two integers K and D, the task is to calculate the minimum possible number of operations required to obtain at least K equal array elements. Each operation involves replacing an element A[i] by A[i] / D. This operation can be performed any number of times. Examples:
9 min read