Minimize count of divisions by D to obtain at least K equal array elements
Last Updated :
08 Jun, 2022
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:
Input: N = 5, A[ ] = {1, 2, 3, 4, 5}, K = 3, D = 2
Output: 2
Explanation:
Step 1: Replace A[3] by A[3] / D, i.e. (4 / 2) = 2. Hence, the array modifies to {1, 2, 3, 2, 5}
Step 2: Replace A[4] by A[4] / D, i.e. (5 / 2) = 2. Hence, the array modifies to {1, 2, 3, 2, 2}
Hence, the modified array has K(= 3) equal elements.
Hence, the minimum number of operations required is 2.
Input: N = 4, A[ ] = {1, 2, 3, 6}, K = 2, D = 3
Output: 1
Explanation:
Replacing A[3] by A[3] / D, i.e. (6 / 3) = 2. Hence, the array modifies to {1, 2, 3, 2}.
Hence, the modified array has K(= 2) equal elements.
Hence, the minimum number of operations required is 1.
Naive Approach:
The simplest approach to solve the problem is to generate every possible subset of the given array and perform the given operation on all elements of this subset. The number of operations required for each subset will be equal to the size of the subset. For each subset, count the number of equal elements and check if count is equal to K. If so, compare the then count with minimum moves obtained so far and update accordingly. Finally, print the minimum moves.
Time Complexity: O(2N *N)
Auxiliary Space: O(N)
Efficient Approach:
Follow the steps below to solve the problem:
- Initialize a 2D vector V, in which, size of a row V[i] denotes the number of array elements that have been reduced to A[i] and every element of the row denotes a count of divisions by D performed on an array element to obtain the number i.
- Traverse the array. For each element A[i], keep dividing it by D until it reduces to 0.
- For each intermediate value of A[i] obtained in the above step, insert the number of divisions required into V[A[i]].
- Once, the above steps are completed for the entire array, iterate over the array V[ ].
- For each V[i], check if the size of V[i] ? K (at most K).
- If V[i] ? K, it denotes that at least K elements in the array have been reduced to i. Sort V[i] and add the first K values, i.e. the smallest K moves required to make K elements equal in the array.
- Compare the sum of K moves with the minimum moves required and update accordingly.
- Once the traversal of the array V[] is completed, print the minimum count of moves obtained finally.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMinimumMoves( int n, int k, int d,
vector< int > a)
{
int MAX = 100000;
vector< int > v[MAX];
for ( int i = 0; i < n; i++) {
int cnt = 0;
v[a[i]].push_back(0);
while (a[i] > 0) {
a[i] /= d;
cnt++;
v[a[i]].push_back(cnt);
}
}
int ans = INT_MAX;
for ( int i = 0; i < MAX; i++) {
if (v[i].size() >= k) {
int move = 0;
sort(v[i].begin(), v[i].end());
for ( int j = 0; j < k; j++) {
move += v[i][j];
}
ans = min(ans, move);
}
}
return ans;
}
int main()
{
int N = 5, K = 3, D = 2;
vector< int > A = { 1, 2, 3, 4, 5 };
cout << getMinimumMoves(N, K, D, A);
return 0;
}
|
Java
import java.util.*;
class GFG{
@SuppressWarnings ( "unchecked" )
static int getMinimumMoves( int n, int k,
int d, int [] a)
{
int MAX = 100000 ;
Vector<Integer> []v = new Vector[MAX];
for ( int i = 0 ; i < v.length; i++)
v[i] = new Vector<Integer>();
for ( int i = 0 ; i < n; i++)
{
int cnt = 0 ;
v[a[i]].add( 0 );
while (a[i] > 0 )
{
a[i] /= d;
cnt++;
v[a[i]].add(cnt);
}
}
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < MAX; i++)
{
if (v[i].size() >= k)
{
int move = 0 ;
Collections.sort(v[i]);
for ( int j = 0 ; j < k; j++)
{
move += v[i].get(j);
}
ans = Math.min(ans, move);
}
}
return ans;
}
public static void main(String[] args)
{
int N = 5 , K = 3 , D = 2 ;
int []A = { 1 , 2 , 3 , 4 , 5 };
System.out.print(getMinimumMoves(N, K, D, A));
}
}
|
Python3
def getMinimumMoves(n, k, d, a):
MAX = 100000
v = []
for i in range ( MAX ):
v.append([])
for i in range (n):
cnt = 0
v[a[i]] + = [ 0 ]
while (a[i] > 0 ):
a[i] / / = d
cnt + = 1
v[a[i]] + = [cnt]
ans = float ( 'inf' )
for i in range ( MAX ):
if ( len (v[i]) > = k):
move = 0
v[i].sort()
for j in range (k):
move + = v[i][j]
ans = min (ans, move)
return ans
if __name__ = = '__main__' :
N = 5
K = 3
D = 2
A = [ 1 , 2 , 3 , 4 , 5 ]
print (getMinimumMoves(N, K, D, A))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int getMinimumMoves( int n, int k,
int d, int [] a)
{
int MAX = 100000;
List< int > []v = new List< int >[MAX];
for ( int i = 0; i < v.Length; i++)
v[i] = new List< int >();
for ( int i = 0; i < n; i++)
{
int cnt = 0;
v[a[i]].Add(0);
while (a[i] > 0)
{
a[i] /= d;
cnt++;
v[a[i]].Add(cnt);
}
}
int ans = int .MaxValue;
for ( int i = 0; i < MAX; i++)
{
if (v[i].Count >= k)
{
int move = 0;
v[i].Sort();
for ( int j = 0; j < k; j++)
{
move += v[i][j];
}
ans = Math.Min(ans, move);
}
}
return ans;
}
public static void Main(String[] args)
{
int N = 5, K = 3, D = 2;
int []A = { 1, 2, 3, 4, 5 };
Console.Write(getMinimumMoves(N, K, D, A));
}
}
|
Javascript
<script>
function getMinimumMoves(n, k, d,a)
{
let MAX = 100000;
let v = new Array(MAX).fill(0).map(()=> new Array());
for (let i = 0; i < n; i++) {
let cnt = 0;
v[a[i]].push(0);
while (a[i] > 0) {
a[i] = Math.floor(a[i] / d);
cnt++;
v[a[i]].push(cnt);
}
}
let ans = Number.MAX_VALUE;
for (let i = 0; i < MAX; i++) {
if (v[i].length >= k) {
let move = 0;
v[i].sort((a,b)=>a-b);
for (let j = 0; j < k; j++) {
move += v[i][j];
}
ans = Math.min(ans, move);
}
}
return ans;
}
let N = 5, K = 3, D = 2;
let A = [ 1, 2, 3, 4, 5 ];
document.write(getMinimumMoves(N, K, D, A), "</br>" );
</script>
|
Time Complexity: O(MlogM), where M is the maximum number taken
Auxiliary Space: O(M)
Share your thoughts in the comments
Please Login to comment...