Minimize count of divisions by 2 required to make all array elements equal
Last Updated :
15 Jun, 2021
Given an array arr[] consisting of N positive integers, the task is to find the minimum count of divisions(integer division) of array elements by 2 to make all array elements the same.
Examples:
Input: arr[] = {3, 1, 1, 3}
Output: 2
Explanation:
Operation 1: Divide arr[0] ( = 3) by 2. The array arr[] modifies to {1, 1, 1, 3}.
Operation 2: Divide arr[3] ( = 3) by 2. The array arr[] modifies to {1, 1, 1, 1}.
Therefore, the count of division operations required is 2.
Input: arr[] = {2, 2, 2}
Output: 0
Approach: The idea to solve the given problem is to find the maximum number to which all the elements in the array can be reduced. Follow the steps below to solve the problem:
- Initialize a variable, say ans, to store the minimum count of division operations required.
- Initialize a HashMap, say M, to store the frequencies of array elements.
- Traverse the array arr[] until any array element arr[i] is found to be greater than 0. Keep dividing arr[i] by 2 and simultaneously update the frequency of the element obtained in the Map M.
- Traverse the HashMap and find the maximum element with frequency N. Store it in maxNumber.
- Again, traverse the array arr[] and find the number of operations required to reduce arr[i] to maxNumber by dividing arr[i] by 2 and add the count of operations to the variable ans.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void makeArrayEqual( int A[], int n)
{
map< int , int > mp;
int max_number = 0;
int ans = 0;
for ( int i = 0; i < n; i++) {
int b = A[i];
while (b) {
mp[b]++;
b /= 2;
}
}
for ( auto x : mp) {
if (x.second == n) {
max_number = x.first;
}
}
for ( int i = 0; i < n; i++) {
int b = A[i];
while (b != max_number) {
ans++;
b /= 2;
}
}
cout << ans;
}
int main()
{
int arr[] = { 3, 1, 1, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
makeArrayEqual(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.Map;
import java.util.HashMap;
class GFG
{
public static void makeArrayEqual( int A[], int n)
{
HashMap<Integer, Integer> map = new HashMap<>();
int max_number = 0 ;
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
int b = A[i];
while (b> 0 ) {
Integer k = map.get(b);
map.put(b, (k == null ) ? 1 : k + 1 );
b /= 2 ;
}
}
for (Map.Entry<Integer, Integer> e :
map.entrySet()) {
if (e.getValue() == n) {
max_number = e.getKey();
}
}
for ( int i = 0 ; i < n; i++) {
int b = A[i];
while (b != max_number) {
ans++;
b /= 2 ;
}
}
System.out.println(ans + " " );
}
public static void main(String[] args)
{
int arr[] = { 3 , 1 , 1 , 3 };
int N = arr.length;
makeArrayEqual(arr, N);
}
}
|
Python3
def makeArrayEqual(A, n):
mp = dict ()
max_number = 0
ans = 0
for i in range (n):
b = A[i]
while (b> 0 ):
if (b in mp):
mp[b] + = 1
else :
mp[b] = mp.get(b, 0 ) + 1
b / / = 2
for key,value in mp.items():
if (value = = n):
max_number = key
for i in range (n):
b = A[i]
while (b ! = max_number):
ans + = 1
b / / = 2
print (ans)
if __name__ = = '__main__' :
arr = [ 3 , 1 , 1 , 3 ]
N = len (arr)
makeArrayEqual(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static void makeArrayEqual( int [] A, int n)
{
Dictionary< int , int > map = new Dictionary< int , int >();
int max_number = 0;
int ans = 0;
for ( int i = 0; i < n; i++) {
int b = A[i];
while (b > 0)
{
if (map.ContainsKey(b))
map[b] ++;
else
map[b]= 1;
b /= 2;
}
}
foreach (KeyValuePair< int , int > e in map)
{
if (e.Value == n) {
max_number = e.Key;
}
}
for ( int i = 0; i < n; i++) {
int b = A[i];
while (b != max_number) {
ans++;
b /= 2;
}
}
Console.Write(ans + " " );
}
public static void Main(String[] args)
{
int [] arr = { 3, 1, 1, 3 };
int N = arr.Length;
makeArrayEqual(arr, N);
}
}
|
Javascript
<script>
function makeArrayEqual(A, n)
{
let mp = new Map();
let max_number = 0;
let ans = 0;
for (let i = 0; i < n; i++)
{
let b = A[i];
while (b)
{
if (mp.has(b))
{
mp.set(b, mp.get(b) + 1)
}
else
{
mp.set(b, 1)
}
b = Math.floor(b / 2);
}
}
for (let x of mp)
{
if (x[1] == n)
{
max_number = x[0];
}
}
for (let i = 0; i < n; i++)
{
let b = A[i];
while (b != max_number)
{
ans++;
b = Math.floor(b / 2);
}
}
document.write(ans);
}
let arr = [ 3, 1, 1, 3 ];
let N = arr.length
makeArrayEqual(arr, N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...