Minimum insertions to make XOR of an Array equal to half of its sum | C ++ 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 ; Calculate the sum and Xor of all the elements ; If the required condition satisfies already , return the original array ; If Xor is already zero , Insert sum ; Otherwise , insert xr and insert sum + xr ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int make_xor_half ( vector < int > & arr ) { int sum = 0 , xr = 0 ; for ( int a : arr ) { sum += a ; xr ^= a ; } if ( 2 * xr == sum ) return -1 ; if ( xr == 0 ) { arr . push_back ( sum ) ; return 1 ; } arr . push_back ( xr ) ; arr . push_back ( sum + xr ) ; return 2 ; } 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 ; }