Make all array elements equal by reducing array elements to half minimum number of times
Last Updated :
17 May, 2021
Given an array arr[] consisting of N integers, the task is to minimize the number of operations required to make all array elements equal by converting Ai to Ai / 2. in each operation
Examples:
Input: arr[] = {3, 1, 1, 3}
Output: 2
Explanation:
Reducing A0 to A0 / 2 modifies arr[] to {1, 1, 1, 3}.
Reducing A3 to A3 / 2 modifies arr[] to {1, 1, 1, 1}.
Therefore, all array elements are equal, Hence, the minimum operations required is 2.
Input: arr[] = {2, 2, 2}
Output: 0
Approach: The idea to solve this problem is to use Greedy Approach. Below are the steps:
- Initialize an auxiliary Map, say mp.
- Traverse the array and for each array element, divide the element by 2 until it reduces to 1, and store the resulting number in the Map.
- Traverse the map and find the maximum element having a frequency equal to N, say mx.
- Again, traverse the array and for each element, divide the element by 2 until it becomes equal to mx and increment count.
- Print count as the minimum number of required operations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int arr[], int N)
{
map< int , int > mp;
for ( int i = 0; i < N; i++) {
int res = arr[i];
while (res) {
mp[res]++;
res /= 2;
}
}
int mx = 1;
for ( auto it : mp) {
if (it.second == N) {
mx = it.first;
}
}
int ans = 0;
for ( int i = 0; i < N; i++) {
int res = arr[i];
while (res != mx) {
ans++;
res /= 2;
}
}
cout << ans;
}
int main()
{
int arr[] = { 3, 1, 1, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
minOperations(arr, N);
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void minOperations( int [] arr, int N)
{
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++) {
int res = arr[i];
if (mp.containsKey(res))
mp.put(res, mp.get(res) + 1 );
else
mp.put(res, 1 );
res /= 2 ;
}
int mx = 1 ;
for (Map.Entry<Integer, Integer> it : mp.entrySet())
{
if (it.getValue() == N)
{
mx = it.getKey();
}
}
int ans = 0 ;
for ( int i = 0 ; i < N; i++)
{
int res = arr[i];
while (res != mx)
{
ans++;
res /= 2 ;
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
int arr[] = { 3 , 1 , 1 , 3 };
int N = arr.length;
minOperations(arr, N);
}
}
|
Python3
def minOperations(arr, N):
mp = {}
for i in range (N):
res = arr[i]
while (res):
if res in mp:
mp[res] + = 1
else :
mp[res] = 1
res / / = 2
mx = 1
for it in mp:
if (mp[it] = = N):
mx = it
ans = 0
for i in range (N):
res = arr[i]
while (res ! = mx):
ans + = 1
res / / = 2
print (ans)
arr = [ 3 , 1 , 1 , 3 ]
N = len (arr)
minOperations(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void minOperations( int [] arr, int N)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
int res = arr[i];
while (res > 0) {
if (mp.ContainsKey(res))
{
mp[res]++;
}
else {
mp[res] = 1;
}
res /= 2;
}
}
int mx = 1;
foreach (KeyValuePair< int , int > it in mp)
{
if (it.Value == N)
{
mx = it.Key;
}
}
int ans = 0;
for ( int i = 0; i < N; i++)
{
int res = arr[i];
while (res != mx)
{
ans++;
res /= 2;
}
}
Console.Write(ans);
}
static void Main()
{
int [] arr = { 3, 1, 1, 3 };
int N = arr.Length;
minOperations(arr, N);
}
}
|
Javascript
<script>
function minOperations(arr, N)
{
var mp = new Map();
for ( var i = 0; i < N; i++) {
var res = arr[i];
while (res) {
if (mp.has(res))
{
mp.set(res, mp.get(res)+1);
}
else
{
mp.set(res, 1);
}
res = parseInt(res/2);
}
}
var mx = 1;
mp.forEach((value, key) => {
if (value == N) {
mx = key;
}
});
var ans = 0;
for ( var i = 0; i < N; i++) {
var res = arr[i];
while (res != mx) {
ans++;
res = parseInt(res/2);
}
}
document.write( ans);
}
var arr = [3, 1, 1, 3];
var N = arr.length;
minOperations(arr, N);
</script>
|
Time Complexity: O(N * log(max(arr[i]))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...