Number whose XOR sum with given array is a given number k
Last Updated :
10 Nov, 2022
Given an array of N numbers and a number K. The task is to insert a number in the given array such that the bitwise XOR of all the elements in the new array equals the given input K.
Examples:
Input: a = {1, 2, 3, 4, 5}, k = 10
Output: 11
Explanation: 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 11 = 10
Input: A[] = { 12, 23, 34, 56, 78 }, k = 6
Output: 73
Approach:
The basic idea is to use the simple XOR property, i.e. if X ^ Y = Z then X ^ Z = Y. Let’s suppose the number to be inserted in array be X such that (A[0] ^ A[1] ^ … ^ A[n – 1]) ^ X = k. Thus, to find X we can use the relation (A[0] ^ A[1] ^ … ^ A[n – 1]) ^ k = X.
Below is the implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findEletobeInserted( int A[], int n, int k)
{
int ans = k;
for ( int i = 0; i < n; i++)
ans ^= A[i];
return ans;
}
int main()
{
int A[] = { 1, 2, 3, 4, 5 };
int n = sizeof (A) / sizeof (A[0]);
int k = 10;
cout << findEletobeInserted(A, n, k)
<< " has to be inserted"
" in the given array to make xor sum of "
<< k << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findEletobeInserted( int A[],
int n, int k)
{
int ans = k;
for ( int i = 0 ; i < n; i++)
ans ^= A[i];
return ans;
}
public static void main (String[] args)
{
int A[] = { 1 , 2 , 3 , 4 , 5 };
int n =A.length;
int k = 10 ;
System.out.println(
findEletobeInserted(A, n, k)
+ " has to be inserted in "
+ "the given array to make"
+ " xor sum of " + k);
}
}
|
Python3
def findEletobeInserted(A, n, k):
ans = k
for i in range (n):
ans ^ = A[i]
return ans
if __name__ = = '__main__' :
A = [ 1 , 2 , 3 , 4 , 5 ]
n = len (A)
k = 10
print (findEletobeInserted(A, n, k),
"has to be inserted in the given" ,
"array to make xor sum of" , k)
|
C#
using System ;
class GFG {
static int findEletobeInserted( int []A,
int n, int k)
{
int ans = k;
for ( int i = 0; i < n; i++)
ans ^= A[i];
return ans;
}
public static void Main ()
{
int []A = { 1, 2, 3, 4, 5 };
int n =A.Length;
int k = 10;
Console.WriteLine(
findEletobeInserted(A, n, k)
+ " has to be inserted in "
+ "the given array to make"
+ " xor sum of " + k);
}
}
|
PHP
<?php
function findEletobeInserted( $A , $n , $k )
{
$ans = $k ;
for ( $i = 0; $i < $n ; $i ++)
$ans ^= $A [ $i ];
return $ans ;
}
$A = array (1, 2, 3, 4, 5);
$n = count ( $A );
$k = 10;
echo findEletobeInserted( $A , $n , $k ) ;
echo " has to be inserted" ;
echo " in the given array to make xor sum of " ;
echo $k , "\n" ;
?>
|
Javascript
<script>
function findEletobeInserted(A, n, k)
{
var ans = k;
for ( var i = 0; i < n; i++)
ans ^= A[i];
return ans;
}
var A = [ 1, 2, 3, 4, 5 ];
var n = A.length;
var k = 10;
document.write(findEletobeInserted(A, n, k) +
" has to be inserted in " +
"the given array to make" +
" xor sum of " + k);
</script>
|
Output
11 has to be inserted in the given array to make xor sum of 10
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...