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++
#include <bits/stdc++.h>
using namespace std;
int makeEqual( int * arr, int n)
{
int fre0[33] = { 0 };
int fre1[33] = { 0 };
for ( int i = 0; i < n; i++) {
int x = arr[i];
for ( int j = 0; j < 33; j++) {
if (x & 1) {
fre1[j] += 1;
}
else {
fre0[j] += 1;
}
x = x >> 1;
}
}
int ans = 0;
for ( int i = 0; i < 33; i++) {
ans += min(fre0[i], fre1[i]);
}
return ans;
}
int main()
{
int arr[] = { 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << makeEqual(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int makeEqual( int arr[], int n)
{
int fre0[] = new int [ 33 ];
int fre1[] = new int [ 33 ];
for ( int i = 0 ; i < n; i++)
{
int x = arr[i];
for ( int j = 0 ; j < 33 ; j++)
{
if ((x & 1 ) != 0 )
{
fre1[j] += 1 ;
}
else
{
fre0[j] += 1 ;
}
x = x >> 1 ;
}
}
int ans = 0 ;
for ( int i = 0 ; i < 33 ; i++)
{
ans += Math.min(fre0[i], fre1[i]);
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 3 , 5 };
int N = arr.length;
System.out.print(makeEqual(arr, N));
}
}
|
Python3
def makeEqual(arr, n):
fre0 = [ 0 ] * 33
fre1 = [ 0 ] * 33
for i in range (n):
x = arr[i]
for j in range ( 33 ):
if (x & 1 ):
fre1[j] + = 1
else :
fre0[j] + = 1
x = x >> 1
ans = 0
for i in range ( 33 ):
ans + = min (fre0[i], fre1[i])
return ans
if __name__ = = '__main__' :
arr = [ 3 , 5 ]
N = len (arr)
print (makeEqual(arr, N))
|
C#
using System;
class GFG {
static int makeEqual( int [] arr, int n)
{
int [] fre0 = new int [33];
int [] fre1 = new int [33];
for ( int i = 0; i < n; i++) {
int x = arr[i];
for ( int j = 0; j < 33; j++) {
if ((x & 1) != 0) {
fre1[j] += 1;
}
else {
fre0[j] += 1;
}
x = x >> 1;
}
}
int ans = 0;
for ( int i = 0; i < 33; i++) {
ans += Math.Min(fre0[i], fre1[i]);
}
return ans;
}
public static void Main()
{
int [] arr = { 3, 5 };
int N = arr.Length;
Console.WriteLine(makeEqual(arr, N));
}
}
|
Javascript
<script>
function makeEqual(arr , n)
{
var fre0 = Array(33).fill(0);
var fre1 = Array(33).fill(0);
for (i = 0; i < n; i++) {
var x = arr[i];
for (j = 0; j < 33; j++) {
if ((x & 1) != 0) {
fre1[j] += 1;
}
else {
fre0[j] += 1;
}
x = x >> 1;
}
}
var ans = 0;
for (i = 0; i < 33; i++) {
ans += Math.min(fre0[i], fre1[i]);
}
return ans;
}
var arr = [ 3, 5 ];
var N = arr.length;
document.write(makeEqual(arr, N));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 May, 2021
Like Article
Save Article