Minimum number of bits of array elements required to be flipped to make all array elements equal
Last Updated :
06 May, 2021
Given an array arr[] consisting of N positive integers, the task is to find the minimum number of bits of array elements required to be flipped to make all array elements equal.
Examples:
Input: arr[] = {3, 5}
Output: 2
Explanation:
Following are the flipping of bits of the array elements required:
- For element arr[0](= 3): Flipping the 3rd bit from the right modifies arr[0] to (= 7(111)2). Now, the array becomes {7, 5}.
- For element arr[0](= 7): Flipping the 2nd bit from the right modifies arr[0] to 5 (= (101)2). Now, the array becomes {5, 5}.
After performing the above operations, all the array elements are equal. Therefore, the total number of flips of bits required is 2.
Input: arr[] = {4, 6, 3, 4, 5}
Output: 5
Approach: The given problem can be solved by modifying the array element in such a way that the number of set bits and unset bits at every position between all array elements. Follow the below steps to solve the problem:
- Initialize two frequency arrays say fre0[] and fre1[] of size 32 for counting the frequency of 0 and 1 for every bit of array elements.
- Traverse the given array and for each array element, arr[i] if the jth bit of arr[i] is a set bit, then increment the frequency of fre1[j] by 1. Otherwise, increment the frequency of fre0[j] by 1.
- After completing the above steps, print the sum of the minimum of fre0[i] and fre1[i] for each bit i over the range [0, 32].
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count minimum number
// of bits required to be flipped
// to make all array elements equal
int makeEqual(int* arr, int n)
{
// Stores the count of unset bits
int fre0[33] = { 0 };
// Stores the count of set bits
int fre1[33] = { 0 };
// Traverse the array
for (int i = 0; i < n; i++) {
int x = arr[i];
// Traverse the bit of arr[i]
for (int j = 0; j < 33; j++) {
// If current bit is set
if (x & 1) {
// Increment fre1[j]
fre1[j] += 1;
}
// Otherwise
else {
// Increment fre0[j]
fre0[j] += 1;
}
// Right shift x by 1
x = x >> 1;
}
}
// Stores the count of total moves
int ans = 0;
// Traverse the range [0, 32]
for (int i = 0; i < 33; i++) {
// Update the value of ans
ans += min(fre0[i], fre1[i]);
}
// Return the minimum number of
// flips required
return ans;
}
// Driver Code
int main()
{
int arr[] = { 3, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << makeEqual(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to count minimum number
// of bits required to be flipped
// to make all array elements equal
static int makeEqual(int arr[], int n)
{
// Stores the count of unset bits
int fre0[] = new int[33];
// Stores the count of set bits
int fre1[] = new int[33];
// Traverse the array
for(int i = 0; i < n; i++)
{
int x = arr[i];
// Traverse the bit of arr[i]
for(int j = 0; j < 33; j++)
{
// If current bit is set
if ((x & 1) != 0)
{
// Increment fre1[j]
fre1[j] += 1;
}
// Otherwise
else
{
// Increment fre0[j]
fre0[j] += 1;
}
// Right shift x by 1
x = x >> 1;
}
}
// Stores the count of total moves
int ans = 0;
// Traverse the range [0, 32]
for(int i = 0; i < 33; i++)
{
// Update the value of ans
ans += Math.min(fre0[i], fre1[i]);
}
// Return the minimum number of
// flips required
return ans;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 5 };
int N = arr.length;
System.out.print(makeEqual(arr, N));
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
# Function to count minimum number
# of bits required to be flipped
# to make all array elements equal
def makeEqual(arr, n):
# Stores the count of unset bits
fre0 = [0]*33
# Stores the count of set bits
fre1 = [0]*33
# Traverse the array
for i in range(n):
x = arr[i]
# Traverse the bit of arr[i]
for j in range(33):
# If current bit is set
if (x & 1):
# Increment fre1[j]
fre1[j] += 1
# Otherwise
else:
# Increment fre0[j]
fre0[j] += 1
# Right shift x by 1
x = x >> 1
# Stores the count of total moves
ans = 0
# Traverse the range [0, 32]
for i in range(33):
# Update the value of ans
ans += min(fre0[i], fre1[i])
# Return the minimum number of
# flips required
return ans
# Driver Code
if __name__ == '__main__':
arr= [3, 5]
N = len(arr)
print(makeEqual(arr, N))
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG {
// Function to count minimum number
// of bits required to be flipped
// to make all array elements equal
static int makeEqual(int[] arr, int n)
{
// Stores the count of unset bits
int[] fre0 = new int[33];
// Stores the count of set bits
int[] fre1 = new int[33];
// Traverse the array
for (int i = 0; i < n; i++) {
int x = arr[i];
// Traverse the bit of arr[i]
for (int j = 0; j < 33; j++) {
// If current bit is set
if ((x & 1) != 0) {
// Increment fre1[j]
fre1[j] += 1;
}
// Otherwise
else {
// Increment fre0[j]
fre0[j] += 1;
}
// Right shift x by 1
x = x >> 1;
}
}
// Stores the count of total moves
int ans = 0;
// Traverse the range [0, 32]
for (int i = 0; i < 33; i++) {
// Update the value of ans
ans += Math.Min(fre0[i], fre1[i]);
}
// Return the minimum number of
// flips required
return ans;
}
// Driver Code
public static void Main()
{
int[] arr = { 3, 5 };
int N = arr.Length;
Console.WriteLine(makeEqual(arr, N));
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// javascript program for the above approach
// Function to count minimum number
// of bits required to be flipped
// to make all array elements equal
function makeEqual(arr , n)
{
// Stores the count of unset bits
var fre0 = Array(33).fill(0);
// Stores the count of set bits
var fre1 = Array(33).fill(0);
// Traverse the array
for (i = 0; i < n; i++) {
var x = arr[i];
// Traverse the bit of arr[i]
for (j = 0; j < 33; j++) {
// If current bit is set
if ((x & 1) != 0) {
// Increment fre1[j]
fre1[j] += 1;
}
// Otherwise
else {
// Increment fre0[j]
fre0[j] += 1;
}
// Right shift x by 1
x = x >> 1;
}
}
// Stores the count of total moves
var ans = 0;
// Traverse the range [0, 32]
for (i = 0; i < 33; i++) {
// Update the value of ans
ans += Math.min(fre0[i], fre1[i]);
}
// Return the minimum number of
// flips required
return ans;
}
// Driver Code
var arr = [ 3, 5 ];
var N = arr.length;
document.write(makeEqual(arr, N));
// This code is contributed by aashish1995
</script>
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Similar Reads
Make all array elements equal by reducing array elements to half minimum number of times 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: 2Explanation: Reducing A0 to A0 / 2 modifies arr[] to {1, 1, 1, 3}. Red
6 min read
Find the minimum number of operations required to make all array elements equal Given an array arr[] of size N. The task is to make all the array elements equal by applying the below operations minimum number of times: Choose a pair of indices (i, j) such that |i - j| = 1 (indices i and j are adjacent) and set arr[i] = arr[i] + |arr[i] - arr[j]|Choose a pair of indices (i, j) s
6 min read
Minimum swaps of similar indexed elements required to make all elements of one of the two given arrays equal Given two arrays A[] and B[] of size N, the task is to count the minimum number of swaps of similar indexed elements required to make one of the two given arrays equal. If it is not possible to make all elements of an array equal, then print -1. Examples: Input: A[] = {3, 4, 1, 3, 1, 3, 4}, B[] = {1
12 min read
Minimum number of bits required to be flipped such that Bitwise OR of A and B is equal to C Given three positives integers A, B, and C, the task is to count the minimum number of flipping of bits required in A and B, such that the Bitwise OR of A and B is equal to C or not. Examples: Input: A = 2, B = 2, C = 3Output: 1Explanation:The binary representation of A is 010, B is 010 and C is 011
9 min read
Minimum operations required to make all elements in an array of first N odd numbers equal Given an array consisting of first N odd numbers, the task is to find the minimum number of operations required to make all the array elements equal by repeatedly selecting a pair and incrementing one element and decrementing the other element in the pair by 1. Examples: Input: N = 3Output: 2Explana
10 min read