Minimum insertions to make XOR of an Array equal to half of its sum
Last Updated :
30 Sep, 2022
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:
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
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
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;
}
|
Python3
def make_xor_half(arr):
sum = 0 ; xr = 0 ;
for a in arr:
sum + = a;
xr ^ = a;
if ( 2 * xr = = sum ):
return - 1 ;
if (xr = = 0 ):
arr.append( sum );
return 1 ;
arr.append(xr);
arr.append( sum + xr);
return 2 ;
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 ]);
|
Java
import java.util.*;
class GFG{
static int make_xor_half(ArrayList<Integer> arr)
{
int sum = 0 , xr = 0 ;
for ( int i = 0 ;
i < arr.size(); i++)
{
int a = arr.get(i);
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<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" );
}
}
|
C#
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 + "\n" );
else if (count == 1)
Console.Write(nums[N] + "\n" );
else
Console.Write(nums[N] + " " +
nums[N + 1] + "\n" );
}
}
|
Javascript
<script>
function make_xor_half(arr)
{
let sum = 0, xr = 0;
for (let a = 0; a < arr.length; a++)
{
sum += arr[a];
xr ^= arr[a];
}
if (2 * xr == sum)
return -1;
if (xr == 0) {
arr.push(sum);
return 1;
}
arr.push(xr);
arr.push(sum + xr);
return 2;
}
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>
|
Time Complexity: O(N) where N is the size of the array.
Auxiliary Space: O(1) it is using constant space for variables
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...