从给定的数组中找到一个位差之和最小的数字
给定一个数组arr[]和一个整数L ,它表示在数组元素的位表示中要考虑的位数。任务是找到一个正整数X ,使得arr[]中所有元素与X的位差之和最小。
例子:
Input: N = 3, L = 5, arr[] = {18, 9, 21}
Output: 17
Explanation: arr[1] = (18)10 = (10010)2, arr[2] = (9)10 = (01001)2, arr[3] = (21)10 = (10101)2
Suppose X = (17)10 = (10001)2.
Difference between arr[1] and X : 2 (Different bits at index 3 and 4)
Difference between arr[2] and X : 2 (Different bits at index 0 and 1)
Difference between arr[3] and X : 1 (Different bits at index 2)
Therefore if X = 17, the sum of bit differences will be 2 + 2 +1 = 5, which is the minimum possible.
Input: N = 3, L = 5 and arr[] = {8, 8, 8}
Output: 8
方法:这个问题可以用下面的思路来解决:
- 在位位置 i,结果 X 中的位应与所有 Array 元素的第 i 位的多数位相同。
- 这可以使用散列来实现。
插图:
Suppose array is {5, 6, 4}
Bit representation of array:
5 = 101
6 = 110
4 = 100
Now lets find the X using positionwise majority bit of Array:
At position 1: majority(1, 1, 1) = 1
At position 2: majority(0, 1, 0) = 0
At position 3: majority(1, 0, 0) = 0
Therefore X formed = 100 = 4
Now lets find the bit difference of X with Array elements:
BitDifference(X, 5) = BitDifference(100, 101) = 1
BitDifference(X, 6) = BitDifference(100, 110) = 1
BitDifference(X, 4) = BitDifference(100, 100) = 0
Sum of bit differences = 2, which will be the least possible.
请按照以下步骤解决给定的问题。
- 初始化一个长度为L的数组freq ,它跟踪在每个给定数组元素的每个位置设置的位数。
- 迭代频率,如果第i个索引的频率大于N/2 ,则保持所需数量的位设置。
- 如果第i个索引的频率小于N/2 ,则将位保持在所需数量。
- 将形成的二进制数转换为十进制数并返回值。
- 打印最终结果
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to find number
// having smallest difference
// with N numbers
int smallestDifference(int N, int L, int arr[])
{
// Initializing freq array
// which keeps tracks of
// number of set bits at every index
int freq[L] = { 0 };
// Making freq map of set bits
for (int i = 0; i < L; i++) {
// Traversing every element
for (int j = 0; j < N; j++) {
// If bit is on then
// updating freq array
if ((arr[j] & 1) > 0) {
freq[i]++;
}
arr[j] >>= 1;
}
}
// Converting binary form of needed
// number into decimal form
int number = 0;
int p = 1;
// Traversing freq array
for (int i = 0; i < L; i++) {
// If frequency of set bit
// is greater than N/2
// then we have to keep it set
// in our answer
if (freq[i] > N / 2) {
number += p;
}
p *= 2;
}
// Returning numbers
// having smallest difference
// among N given numbers
return number;
}
// Driver Code
int main()
{
int N = 3;
int L = 5;
int arr[] = { 18, 9, 21 };
// Function call
int number = smallestDifference(N, L, arr);
cout << number << endl;
return 0;
}
Java
// Java program for above approach
import java.util.*;
class GFG {
// Function to find number
// having smallest difference
// with N numbers
public static int smallestDifference(int N, int L,
int[] arr)
{
// Initializing freq array
// which keeps tracks of
// number of set bits at every index
int[] freq = new int[L];
// Making freq map of set bits
for (int i = 0; i < L; i++) {
// Traversing every element
for (int j = 0; j < N; j++) {
// If bit is on then
// updating freq array
if ((arr[j] & 1) > 0) {
freq[i]++;
}
arr[j] >>= 1;
}
}
// Converting binary form of needed
// number into decimal form
int number = 0;
int p = 1;
// Traversing freq array
for (int i = 0; i < L; i++) {
// If frequency of set bit
// is greater than N/2
// then we have to keep it set
// in our answer
if (freq[i] > N / 2) {
number += p;
}
p *= 2;
}
// Returning numbers
// having smallest difference
// among N given numbers
return number;
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
int L = 5;
int[] arr = { 18, 9, 21 };
// Function call
int number = smallestDifference(N, L, arr);
System.out.println(number);
}
}
Python
# Python program for above approach
# Function to find number
# having smallest difference
# with N numbers
def smallestDifference(N, L, arr):
# Initializing freq array
# which keeps tracks of
# number of set bits at every index
freq = []
for i in range(0, L):
freq.append(0)
# Making freq map of set bits
for i in range(0, L):
# Traversing every element
for j in range(0, N):
# If bit is on then
# updating freq array
if ((arr[j] & 1) > 0):
freq[i] += 1
arr[j] >>= 1
# Converting binary form of needed
# number into decimal form
number = 0
p = 1
# Traversing freq array
for i in range(0, L):
# If frequency of set bit
# is greater than N/2
# then we have to keep it set
# in our answer
if (freq[i] > N // 2):
number += p
p *= 2
# Returning numbers
# having smallest difference
# among N given numbers
return number
# Driver Code
N = 3
L = 5
arr = [18, 9, 21]
# Function call
number = smallestDifference(N, L, arr)
print(number)
# This code is contrbuted by Samim Hossain Mondal.
C#
using System;
public class GFG{
// Function to find number
// having smallest difference
// with N numbers
public static int smallestDifference(int N, int L,
int[] arr)
{
// Initializing freq array
// which keeps tracks of
// number of set bits at every index
int[] freq = new int[L];
// Making freq map of set bits
for (int i = 0; i < L; i++) {
// Traversing every element
for (int j = 0; j < N; j++) {
// If bit is on then
// updating freq array
if ((arr[j] & 1) > 0) {
freq[i]++;
}
arr[j] >>= 1;
}
}
// Converting binary form of needed
// number into decimal form
int number = 0;
int p = 1;
// Traversing freq array
for (int i = 0; i < L; i++) {
// If frequency of set bit
// is greater than N/2
// then we have to keep it set
// in our answer
if (freq[i] > N / 2) {
number += p;
}
p *= 2;
}
// Returning numbers
// having smallest difference
// among N given numbers
return number;
}
// Driver Code
static public void Main ()
{
int N = 3;
int L = 5;
int[] arr = { 18, 9, 21 };
// Function call
int number = smallestDifference(N, L, arr);
Console.Write(number);
}
}
// This code is contributed by hrithikgarg03188.
Javascript
17
时间复杂度: O(N*L)
辅助空间: O(L)