📌  相关文章
📜  通过对数组的所有元素应用XOR操作来最小化数组总和

📅  最后修改于: 2021-04-29 14:45:30             🧑  作者: Mango

给定一个由N个整数元素组成的数组arr [] ,任务是选择一个元素X ,并对具有X的数组的每个元素进行XOR操作,以使数组总和最小化。

方法:任务是找到元素X ,我们必须对每个元素进行xor运算。

  • 将每个数字转换为二进制形式,并根据数组中元素中每个位的位置更新数组中位(0或1)的频率。
  • 现在,遍历数组并检查索引处的元素是否大于n / 2(对于“ n”个元素,我们检查设置位是否在索引处大于n / 2),随后我们获得元素“ X”
  • 现在,对所有元素取“ X”的xor并返回总和。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
const int MAX = 25;
  
// Function to return the minimized sum
int getMinSum(int arr[], int n)
{
    int bits_count[MAX], max_bit = 0, sum = 0, ans = 0;
  
    memset(bits_count, 0, sizeof(bits_count));
  
    // To store the frequency
    // of bit in every element
    for (int d = 0; d < n; d++) {
        int e = arr[d], f = 0;
        while (e > 0) {
            int rem = e % 2;
            e = e / 2;
            if (rem == 1) {
                bits_count[f] += rem;
            }
            f++;
        }
        max_bit = max(max_bit, f);
    }
  
    // Finding element X
    for (int d = 0; d < max_bit; d++) {
        int temp = pow(2, d);
        if (bits_count[d] > n / 2)
            ans = ans + temp;
    }
  
    // Taking XOR of elements and finding sum
    for (int d = 0; d < n; d++) {
        arr[d] = arr[d] ^ ans;
        sum = sum + arr[d];
    }
    return sum;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 5, 7, 11, 15 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << getMinSum(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG {
  
    static int MAX = 25;
  
    // Function to return the minimized sum
    static int getMinSum(int arr[], int n)
    {
        int bits_count[] = new int[MAX],
            max_bit = 0, sum = 0, ans = 0;
  
        // To store the frequency
        // of bit in every element
        for (int d = 0; d < n; d++) {
            int e = arr[d], f = 0;
            while (e > 0) {
                int rem = e % 2;
                e = e / 2;
                if (rem == 1) {
                    bits_count[f] += rem;
                }
                f++;
            }
            max_bit = Math.max(max_bit, f);
        }
  
        // Finding element X
        for (int d = 0; d < max_bit; d++) {
            int temp = (int)Math.pow(2, d);
            if (bits_count[d] > n / 2)
                ans = ans + temp;
        }
  
        // Taking XOR of elements and finding sum
        for (int d = 0; d < n; d++) {
            arr[d] = arr[d] ^ ans;
            sum = sum + arr[d];
        }
        return sum;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 3, 5, 7, 11, 15 };
        int n = arr.length;
        System.out.println(getMinSum(arr, n));
    }
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
MAX = 25; 
  
# Function to return the minimized sum 
def getMinSum(arr, n) :
    bits_count = [0]* MAX
    max_bit = 0; sum = 0; ans = 0; 
  
    # To store the frequency 
    # of bit in every element 
    for d in range(n) :
        e = arr[d]; f = 0; 
        while (e > 0) :
            rem = e % 2; 
            e = e // 2; 
            if (rem == 1) :
                bits_count[f] += rem; 
                  
            f += 1 
              
        max_bit = max(max_bit, f); 
      
  
    # Finding element X 
    for d in range(max_bit) :
        temp = pow(2, d); 
          
        if (bits_count[d] > n // 2) :
            ans = ans + temp; 
  
  
    # Taking XOR of elements and finding sum 
    for d in range(n) : 
        arr[d] = arr[d] ^ ans; 
        sum = sum + arr[d]; 
      
    return sum 
      
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 3, 5, 7, 11, 15 ]; 
    n = len(arr); 
  
    print(getMinSum(arr, n))
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
  
class GFG {
  
    static int MAX = 25;
  
    // Function to return the minimized sum
    static int getMinSum(int[] arr, int n)
    {
        int[] bits_count = new int[MAX];
        int max_bit = 0, sum = 0, ans = 0;
  
        // To store the frequency
        // of bit in every element
        for (int d = 0; d < n; d++) {
            int e = arr[d], f = 0;
            while (e > 0) {
                int rem = e % 2;
                e = e / 2;
                if (rem == 1) {
                    bits_count[f] += rem;
                }
                f++;
            }
            max_bit = Math.Max(max_bit, f);
        }
  
        // Finding element X
        for (int d = 0; d < max_bit; d++) {
            int temp = (int)Math.Pow(2, d);
            if (bits_count[d] > n / 2)
                ans = ans + temp;
        }
  
        // Taking XOR of elements and finding sum
        for (int d = 0; d < n; d++) {
            arr[d] = arr[d] ^ ans;
            sum = sum + arr[d];
        }
        return sum;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int[] arr = { 3, 5, 7, 11, 15 };
        int n = arr.Length;
        Console.WriteLine(getMinSum(arr, n));
    }
}
  
/* This code contributed by PrinciRaj1992 */


输出:
26