给定一个由N 个正整数组成的数组arr[] ,任务是找到使所有数组元素相等所需翻转的数组元素的最小位数。
例子:
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
方法:可以通过修改数组元素来解决给定的问题,即在所有数组元素之间的每个位置处设置位和未设置位的数量。请按照以下步骤解决问题:
- 初始化两个频率数组,比如大小为32 的fre0[]和fre1[] ,用于对数组元素的每一位计算 0 和 1 的频率。
- 遍历给定的阵列和用于每个数组元素,ARR [I]如果第i比特ARR的第j [i]是一组位,然后通过递增1 fre1 [j]的频率。否则,将fre0[j]的频率增加1 。
- 完成上述步骤后,为[0, 32]范围内的每个位i打印fre0[i]和fre1[i]的最小值之和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出:
2
时间复杂度: O(N * log N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live