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

📅  最后修改于: 2021-10-26 06:40:49             🧑  作者: Mango

给定一个由N 个整数元素组成的数组arr[] ,任务是选择一个元素X并对数组的每个元素与X应用 XOR 运算,以使数组和最小化。

方法:任务是找到我们必须对每个元素进行异或的元素X。

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

下面是上述方法的实现:

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 */


Javascript


输出:
26

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程