Count of elements to be inserted to make Array sum twice the XOR of Array
Last Updated :
18 Jun, 2022
Given an array arr[] of size N, the task is to find the minimum number of elements that need to be inserted into the array so that the sum of the array is equal to two times the XOR of the array.
Examples:
Input: arr[] = {1, 2, 3, 6}
Output: 0
Explanation:
Xor = (1 ^ 2 ^ 3 ^ 6) = 6 and sum of the array = 12.
The required condition (sum = 2 * Xor) satisfies. So no need to insert more elements.
Input: arr[] = {1, 2, 3}
Output: 1
Explanation:
Xor = (1 ^ 2 ^ 3) = 0 and sum of the array = (1 + 2 + 3) = 6.
Here, we insert one element {6} into the array. Now, NewXor = (0 ^ 6) = 6 and NewSum = (6 + 6) = 12.
Hence, NewSum = 2*NewXor.
So, the given condition satisfies.
Approach: The idea is to compute the following steps in order to find the answer:
- Initially, we find the sum of the array and the XOR of the array.
- Now, we check if the given condition satisfies or not. If it satisfies, then print 0 as we need not insert any element.
- Now, check if the XOR is equal to 0 or not. If it is, then the element that needs to be inserted into the array is the sum of all the elements of the array.
- This is because, by inserting the sum, the new XOR becomes (0 ^ sum = sum) and the sum of the array becomes sum + sum = 2 * sum. Therefore the condition satisfies.
- Else, we add two more elements which are XOR and (sum + XOR). This is because:
NewXor = (Xor ^ (sum + Xor) ^ Xor) = Sum + Xor.
NewSum = (Sum + (Sum + Xor) + Xor) = 2 * (Sum + Xor) = 2 * NewXor
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void insert_element( int a[], int n)
{
int Xor = 0;
int Sum = 0;
for ( int i = 0; i < n; i++)
{
Xor ^= a[i];
Sum += a[i];
}
if (Sum == 2 * Xor)
{
cout << "0" << endl;
return ;
}
if (Xor == 0)
{
cout << "1" << endl;
cout << Sum << endl;
return ;
}
int num1 = Sum + Xor;
int num2 = Xor;
cout << "2" ;
cout << num1 << " "
<< num2 << endl;
}
int main()
{
int a[] = {1, 2, 3};
int n = sizeof (a) / sizeof (a[0]);
insert_element(a, n);
}
|
Java
class GFG{
static void insert_element( int a[], int n)
{
int Xor = 0 ;
int Sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
Xor ^= a[i];
Sum += a[i];
}
if (Sum == 2 * Xor)
{
System.out.println( "0" );
return ;
}
if (Xor == 0 )
{
System.out.println( "1" );
System.out.println(Sum);
return ;
}
int num1 = Sum + Xor;
int num2 = Xor;
System.out.print( "2" );
System.out.println(num1 + " " + num2);
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 3 };
int n = a.length;
insert_element(a, n);
}
}
|
Python3
def insert_element(a, n):
Xor = 0
Sum = 0
for i in range (n):
Xor^ = a[i]
Sum + = a[i]
if ( Sum = = 2 * Xor):
print ( 0 )
return
if (Xor = = 0 ):
print ( 1 )
print ( Sum )
return
num1 = Sum + Xor
num2 = Xor
print ( 2 )
print (num1, num2)
if __name__ = = "__main__" :
a = [ 1 , 2 , 3 ]
n = len (a)
insert_element(a, n)
|
C#
using System;
class GFG{
static void insert_element( int [] a, int n)
{
int Xor = 0;
int Sum = 0;
for ( int i = 0; i < n; i++)
{
Xor ^= a[i];
Sum += a[i];
}
if (Sum == 2 * Xor)
{
Console.Write( "0" );
return ;
}
if (Xor == 0)
{
Console.Write( "1" + '\n' );
Console.Write(Sum);
return ;
}
int num1 = Sum + Xor;
int num2 = Xor;
Console.Write( "2" );
Console.Write(num1 + " " + num2);
}
public static void Main( string [] args)
{
int [] a = {1, 2, 3};
int n = a.Length;
insert_element(a, n);
}
}
|
Javascript
<script>
function insert_element(a, n)
{
let Xor = 0;
let Sum = 0;
for (let i = 0; i < n; i++)
{
Xor ^= a[i];
Sum += a[i];
}
if (Sum == 2 * Xor)
{
document.write( "0" + "</br>" );
return ;
}
if (Xor == 0)
{
document.write( "1" + "</br>" );
document.write(Sum + "</br>" );
return ;
}
let num1 = Sum + Xor;
let num2 = Xor;
document.write( "2" + "</br>" );
document.write(num1 + " " + num2 + "</br>" );
}
let a = [1, 2, 3];
let n = a.length;
insert_element(a, n);
</script>
|
Time Complexity : O(n) ,as we are traversing once in an array.
Space Complexity : O(1) ,as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...