📌  相关文章
📜  使数组的XOR等于其总和的一半的最小插入

📅  最后修改于: 2021-06-26 21:57:57             🧑  作者: Mango

给定一个正整数数组,任务是找到要在该数组中进行插入的最小次数,以使该数组的XOR等于其总和的一半,即2 *所有元素的Xor =所有元素的总和
例子:

方法:
为了解决这个问题,我们需要关注XOR的两个基本属性:

  • A xor A = 0
  • 异或0 = A

我们需要按照以下步骤解决问题:

  • 计算所有数组元素的总和(S)和所有元素的异或(X)。如果S == 2 * X ,则无需更改数组。在这种情况下,打印-1。
  • 否则,请执行以下操作:
    1. 如果X = 0 ,只需将S插入数组。现在,XOR为S ,总和为2S
    2. 否则,将X添加到数组以使数组的新Xor等于0。然后,在数组中插入S + X。现在,总和为2(S + X) ,异或为S + X

下面是上述方法的实现。

C++
// C++ Program to make XOR of
// of all array elements equal
// to half of its sum
// by minimum insertions
 
#include 
using namespace std;
 
// Function to make XOR of the
// array equal to half of its sum
int make_xor_half(vector& arr)
{
    int sum = 0, xr = 0;
 
    // Calculate the sum and
    // Xor of all the elements
    for (int a : arr) {
        sum += a;
        xr ^= a;
    }
 
    // If the required condition
    // satisfies already, return
    // the original array
    if (2 * xr == sum)
        return -1;
 
    // If Xor is already zero,
    // Insert sum
    if (xr == 0) {
        arr.push_back(sum);
        return 1;
    }
 
    // Otherwise, insert xr
    // and insert sum + xr
    arr.push_back(xr);
    arr.push_back(sum + xr);
    return 2;
}
 
// Driver Code
int main()
{
 
    int N = 7;
    vector nums
        = { 3, 4, 7, 1, 2, 5, 6 };
 
    int count = make_xor_half(nums);
 
    if (count == -1)
        cout << "-1" << endl;
    else if (count == 1)
        cout << nums[N] << endl;
    else
        cout << nums[N] << " "
             << nums[N + 1] << endl;
 
    return 0;
}


Python3
# Python3 program to make XOR of
# of all array elements equal to 
# half of its sum by minimum 
# insertions
 
# Function to make XOR of the
# array equal to half of its sum
def make_xor_half(arr):
 
    sum = 0; xr = 0;
 
    # Calculate the sum and
    # Xor of all the elements
    for a in arr:
        sum += a;
        xr ^= a;
 
    # If the required condition
    # satisfies already, return
    # the original array
    if (2 * xr == sum):
        return -1;
 
    # If Xor is already zero,
    # Insert sum
    if (xr == 0):
        arr.append(sum);
        return 1;
 
    # Otherwise, insert xr
    # and insert sum + xr
    arr.append(xr);
    arr.append(sum + xr);
    return 2;
 
# Driver code
if __name__ == "__main__":
 
    N = 7;
    nums = [ 3, 4, 7, 1, 2, 5, 6 ];
    count = make_xor_half(nums);
 
    if (count == -1):
        print("-1");
         
    elif (count == 1):
        print(nums[N]);
         
    else:
        print(nums[N], nums[N + 1]);
 
# This code is contributed by AnkitRai01


Java
// Java program to make XOR of all
// array elements equal to half
// of its sum by minimum insertions
import java.util.*;
 
class GFG{
   
// Function to make XOR of the
// array equal to half of its sum
static int make_xor_half(ArrayList arr)
{
  int sum = 0, xr = 0;
 
  // Calculate the sum and
  // Xor of all the elements
  for (int i = 0;
           i < arr.size(); i++) 
  {
    int a = arr.get(i);
    sum += a;
    xr ^= a;
  }
 
  // If the required condition
  // satisfies already, return
  // the original array
  if (2 * xr == sum)
    return -1;
 
  // If Xor is already zero,
  // Insert sum
  if (xr == 0)
  {
    arr.add(sum);
    return 1;
  }
 
  // Otherwise, insert xr
  // and insert sum + xr
  arr.add(xr);
  arr.add(sum + xr);
  return 2;
}
 
// Driver code
public static void main(String[] args)
{
  int N = 7;
  ArrayList nums =
  new ArrayList(
      Arrays.asList(3, 4, 7,
                    1, 2, 5, 6));
 
  int count = make_xor_half(nums);
 
  if (count == -1)
    System.out.print(-1 + "\n");
  else if (count == 1)
    System.out.print(nums.get(N) + "\n");
  else
    System.out.print(nums.get(N) + " " +
                     nums.get(N + 1) + "\n");
}
}
 
// This code is contributed by Chitranayal


C#
// C# program to make XOR of all
// array elements equal to half
// of its sum by minimum insertions
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
   
// Function to make XOR of the
// array equal to half of its sum
static int make_xor_half(ArrayList arr)
{
    int sum = 0, xr = 0;
  
    // Calculate the sum and
    // Xor of all the elements
    foreach(int a in arr)
    {
        sum += a;
        xr ^= a;
    }
  
    // If the required condition
    // satisfies already, return
    // the original array
    if (2 * xr == sum)
        return -1;
  
    // If Xor is already zero,
    // Insert sum
    if (xr == 0)
    {
        arr.Add(sum);
        return 1;
    }
  
    // Otherwise, insert xr
    // and insert sum + xr
    arr.Add(xr);
    arr.Add(sum + xr);
    return 2;
}
   
// Driver code
public static void Main(string[] args)
{
    int N = 7;
    ArrayList nums = new ArrayList(){ 3, 4, 7, 1,
                                      2, 5, 6 };
  
    int count = make_xor_half(nums);
  
    if (count == -1)
        Console.Write(-1 + "\n");
    else if (count == 1)
        Console.Write(nums[N] + "\n");
    else
        Console.Write(nums[N] + " " +
                      nums[N + 1] + "\n");
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
28

时间复杂度: O(N) ,其中N是数组的大小。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。