Minimum insertions to make XOR of an Array equal to half of its sum | C # program to make XOR 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
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int make_xor_half ( ArrayList arr ) { int sum = 0 , xr = 0 ; foreach ( int a in arr ) { sum += a ; xr ^= a ; } if ( 2 * xr == sum ) return - 1 ; if ( xr == 0 ) { arr . Add ( sum ) ; return 1 ; } arr . Add ( xr ) ; arr . Add ( sum + xr ) ; return 2 ; } 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 + " STRNEWLINE " ) ; else if ( count == 1 ) Console . Write ( nums [ N ] + " STRNEWLINE " ) ; else Console . Write ( nums [ N ] + " ▁ " + nums [ N + 1 ] + " STRNEWLINE " ) ; } }