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 condition2 * Xor of all elements = Sum of all elementsis 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 condition2 * Xor of all elements = Sum of all elementsis 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:
- If
**X = 0**, just insert**S**into the array. Now, the XOR is**S**, and the sum is**2S**. - 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**

- If

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

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

28

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

## Recommended Posts:

- Reorder an array such that sum of left half is not equal to sum of right half
- Find the XOR of first half and second half elements of an array
- Minimum insertions to make a Co-prime array
- Minimum array insertions required to make consecutive difference <= K
- Find the sum of the first half and second half elements of an array
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Find prime numbers in the first half and second half of an array
- Sort first half in ascending and second half in descending order | 1
- Sort first half in ascending and second half in descending order | Set 2
- Count number of rotated strings which have more number of vowels in the first half than second half
- Number of subarrays such that XOR of one half is equal to the other
- Minimum Bitwise XOR operations to make any two array elements equal
- Count subarrays with sum equal to its XOR value
- Minimize insertions in an array to obtain all sums upto N
- Count of operations to make all elements of array a[] equal to its min element by performing a[i] – b[i]
- Count of elements to be inserted to make Array sum twice the XOR of Array
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Find if array has an element whose value is half of array sum
- Minimum operations to make XOR of array zero
- Find a partition point in array to maximize its xor sum

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.