📜  使数组异或为零的最小操作

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

使数组异或为零的最小操作

我们得到一个包含 n 个元素的数组。任务是对整个数组进行 XOR 0。我们可以执行以下操作来实现这一点。

  1. 我们可以选择任何一个元素。
  2. 选择一个元素后,我们可以将其递增或递减 1。

我们需要找到所选元素所需的最小增量/减量操作数,以使整个数组的 XOR 和为零。

例子:

Input : arr[] = {2, 4, 8}
Output : Element = 8, 
         Operation required = 2
Explanation : Select 8 as element and perform 2 
              time decrement on it. So that it
              became 6, Now our array is {2, 4, 6} 
              whose XOR sum is 0.

Input : arr[] = {1, 1, 1, 1}
Output : Element = 1, 
         Operation required = 0
Explanation : Select any of 1 and you have already
              your XOR sum = 0. So, no operation 
              required.

天真的方法:选择一个元素,然后找到数组其余部分的异或。如果该元素等于获得的 XOR,那么我们对整个数组的 XOR 应该变为零。现在,我们的成本将是所选元素和获得的 XOR 之间的绝对差。这个寻找成本的过程将为每个元素完成,从而导致 (n^2) 的时间复杂度。
高效方法:求整个数组的异或。现在,假设我们选择了元素 arr[i],那么该元素所需的成本将是绝对的(arr[i]-(XORsum^arr[i]))。计算每个元素的这些绝对值的最小值将是我们所需的最小操作,与所需的最小操作相对应的元素将是我们选择的元素。

C++
// CPP to find min cost to make
// XOR of whole array zero
#include 
using namespace std;
 
// function to find min cost
void minCost(int arr[], int n)
{
    int cost = INT_MAX;
    int element;
 
    // calculate XOR sum of array
    int XOR = 0;
    for (int i = 0; i < n; i++)
        XOR ^= arr[i];
 
    // find the min cost and element corresponding
    for (int i = 0; i < n; i++) {
        if (cost > abs((XOR ^ arr[i]) - arr[i])) {
            cost = abs((XOR ^ arr[i]) - arr[i]);
            element = arr[i];
        }
    }
 
    cout << "Element = " << element << endl;
    cout << "Operation required = " << abs(cost);
}
 
// driver program
int main()
{
    int arr[] = { 2, 8, 4, 16 };
    int n = sizeof(arr) / sizeof(arr[0]);
    minCost(arr, n);
    return 0;
}


Java
// JAVA program to find min cost to make
// XOR of whole array zero
import java.lang.*;
 
class GFG
{
    // function to find min cost
    static void minCost(int[] arr, int n)
    {
        int cost = Integer.MAX_VALUE;
        int element=0;
 
        // calculate XOR sum of array
        int XOR = 0;
        for (int i = 0; i < n; i++)
            XOR ^= arr[i];
 
        // find the min cost and element
        // corresponding
        for (int i = 0; i < n; i++) {
            if (cost > Math.abs((XOR ^ arr[i])
                                - arr[i])) {
                cost = Math.abs((XOR ^ arr[i]) -
                                       arr[i]);
                element = arr[i];
            }
        }
 
    System.out.println("Element = " + element);
    System.out.println("Operation required = "+
                             Math.abs(cost));
    }
 
    // driver program
    public static void main (String[] args)
    {
        int[] arr = { 2, 8, 4, 16 };
        int n = arr.length;
        minCost(arr, n);
    }
}
/* This code is contributed by Kriti Shukla */


Python3
# python to find min cost to make
# XOR of whole array zero
 
# function to find min cost
def minCost(arr,n):
     
    cost = 999999;
     
    # calculate XOR sum of array
    XOR = 0;
    for i in range(0, n):
        XOR ^= arr[i];
 
    # find the min cost and element
    # corresponding
    for i in range(0,n):
        if (cost > abs((XOR ^ arr[i]) - arr[i])):
            cost = abs((XOR ^ arr[i]) - arr[i])
            element = arr[i]
 
    print("Element = ", element)
    print("Operation required = ", abs(cost))
 
 
# driver program
arr = [ 2, 8, 4, 16 ]
n = len(arr)
minCost(arr, n)
 
# This code is contributed by Sam007


C#
// C# program to find min cost to
// make XOR of whole array zero
using System;
 
class GFG
{
    // function to find min cost
    static void minCost(int []arr, int n)
    {
        int cost = int.MaxValue;
        int element=0;
 
        // calculate XOR sum of array
        int XOR = 0;
        for (int i = 0; i < n; i++)
            XOR ^= arr[i];
 
        // find the min cost and
        // element corresponding
        for (int i = 0; i < n; i++)
        {
            if (cost > Math.Abs((XOR ^ arr[i]) - arr[i]))
            {
                cost = Math.Abs((XOR ^ arr[i]) - arr[i]);
                element = arr[i];
            }
        }
 
    Console.WriteLine("Element = " + element);
    Console.Write("Operation required = "+
                          Math.Abs(cost));
    }
 
    // Driver program
    public static void Main ()
    {
        int []arr = {2, 8, 4, 16};
        int n = arr.Length;
        minCost(arr, n);
    }
}
 
// This code is contributed by nitin mittal.


PHP
 abs(($XOR ^ $arr[$i]) -
                                 $arr[$i]))
        {
            $cost = abs(($XOR ^ $arr[$i]) -
                                 $arr[$i]);
            $element = $arr[$i];
        }
    }
 
    echo "Element = " , $element ,"\n";
    echo "Operation required = " , abs($cost);
}
 
// Driver Code
$arr = array(2, 8, 4, 16) ;
$n = count($arr);
minCost($arr, $n);
 
// This code is contributed by vt_m.
?>


Javascript


输出:

Element = 16
Operation required = 2