Minimum insertions to make XOR of an Array equal to half of its sum

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

28

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

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01