Skip to content
Related Articles

Related Articles

Minimum insertions to make XOR of an Array equal to half of its sum
  • Last Updated : 30 Apr, 2021

Given an array of positive integers, the task is to find the minimum number of insertions to be done in the array, to make the XOR of the array equal to half of its sum, i.e. 2 * Xor of all elements = Sum of all elements
Examples: 

Input: arr[] = {1 2 3 4 5} 
Output: 1 16 
Explanation: 
In the modified array {1 2 3 4 5 1 16}, 
Sum = 1 + 2 + 3 + 4 + 5 + 1 + 16 = 32 
Xor = 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 1 ^ 16 = 16 
And, 2 * 16 == 32 
Thus, the condition 2 * Xor of all elements = Sum of all elements is satisfied.

Input: 7 11 3 25 51 32 9 29 
Output: 17 184 
Explanation: 
In the modified array { 7 11 3 25 51 32 9 29 17 184} 
Sum = 7 + 11 + 3 + 25 + 51 + 32 + 9 + 29 + 17 + 184 = 368 
Xor = 7 ^ 11 ^ 3 ^ 25 ^ 51 ^ 32 ^ 9 ^ 29 ^ 17 ^ 184 = 184 
And, 2 * 184 == 368 
Thus, the condition 2 * Xor of all elements = Sum of all elements is satisfied. 

Approach: 
To solve the problem, we need to focus on the two basic properties of XOR: 

  • A xor A = 0
  • A xor 0 = A

We need to follow the steps below to solve the problem: 



  • Calculate the Sum of all array elements(S) and the Xor of all elements (X). If S == 2*X, no change in array is required. Print -1 for this case.
  • Otherwise, do the following: 
    1. If X = 0, just insert S into the array. Now, the XOR is S, and the sum is 2S.
    2. Otherwise, Add X to the array to make the new Xor of the array equal to 0. Then, insert S+X in the array. Now, the Sum is 2(S+X) and the Xor is S+X

Below is the implementation of the above approach.

C++




// C++ Program to make XOR of
// of all array elements equal
// to half of its sum
// by minimum insertions
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to make XOR of the
// array equal to half of its sum
int make_xor_half(vector<int>& 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<int> 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<Integer> 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<Integer> nums =
  new ArrayList<Integer>(
      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




<script>
 
// Javascript 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
function make_xor_half(arr)
{
    let sum = 0, xr = 0;
 
    // Calculate the sum and
    // Xor of all the elements
    for (let a = 0; a < arr.length; a++)
    {
        sum += arr[a];
        xr ^= arr[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(sum);
        return 1;
    }
 
    // Otherwise, insert xr
    // and insert sum + xr
    arr.push(xr);
    arr.push(sum + xr);
    return 2;
}
 
// Driver Code
 
    let N = 7;
    let nums
        = [ 3, 4, 7, 1, 2, 5, 6 ];
 
    let count = make_xor_half(nums);
 
    if (count == -1)
        document.write("-1<br>");
    else if (count == 1)
        document.write(nums[N] + "<br>");
    else
        document.write(nums[N] + " "
             + nums[N + 1]);
 
</script>
Output: 
28

 

Time Complexity: O(N) where N is the size of the array. 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up
Recommended Articles
Page :