📜  从给定的数组中找到一个位差之和最小的数字

📅  最后修改于: 2022-05-13 01:56:09.019000             🧑  作者: Mango

从给定的数组中找到一个位差之和最小的数字

给定一个数组arr[]和一个整数L ,它表示在数组元素的位表示中要考虑的位数。任务是找到一个正整数X ,使得arr[]中所有元素与X的位差之和最小。

例子:

方法:这个问题可以用下面的思路来解决:

  • 在位位置 i,结果 X 中的位应与所有 Array 元素的第 i 位的多数位相同。
  • 这可以使用散列来实现。

插图:

请按照以下步骤解决给定的问题。  

  • 初始化一个长度为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)