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++
// CPP Program to find the number
// whose XOR sum with given array is
// equal to a given number k
#include <bits/stdc++.h>
using namespace std;
// This function returns the number to
// be inserted in the given array
int findEletobeInserted(int A[], int n, int k)
{
// initialise the answer with k
int ans = k;
for (int i = 0; i < n; i++)
ans ^= A[i]; // XOR of all elements in the array
return ans;
}
// Driver Code to test above function
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
// Java Program to find the number
// whose XOR sum with given array is
// equal to a given number k
import java.io.*;
class GFG {
// This function returns the number to
// be inserted in the given array
static int findEletobeInserted(int A[],
int n, int k)
{
// initialise the answer with k
int ans = k;
for (int i = 0; i < n; i++)
// XOR of all elements in
// the array
ans ^= A[i];
return ans;
}
// Driver Code to test above function
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);
}
}
// This code is contributed by anuj_67.
Python3
# Python 3 Program to find the number
# whose XOR sum with given array is
# equal to a given number k
# This function returns the number to
# be inserted in the given array
def findEletobeInserted(A, n, k):
# initialise the answer with k
ans = k
for i in range(n):
ans ^= A[i] # XOR of all elements
# in the array
return ans
# Driver Code
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)
# This code is contributed by
# Surendra_Gangwar
C#
// C# Program to find the number
// whose XOR sum with given array is
// equal to a given number k
using System ;
class GFG {
// This function returns the number to
// be inserted in the given array
static int findEletobeInserted(int []A,
int n, int k)
{
// initialise the answer with k
int ans = k;
for (int i = 0; i < n; i++)
// XOR of all elements in
// the array
ans ^= A[i];
return ans;
}
// Driver Code to test above function
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);
}
}
// This code is contributed by anuj_67.
PHP
<?php
// PHP Program to find the number
// whose XOR sum with given array is
// equal to a given number k
// This function returns the number to
// be inserted in the given array
function findEletobeInserted($A, $n, $k)
{
// initialise the answer with k
$ans = $k;
for ( $i = 0; $i < $n; $i++)
// XOR of all elements
// in the array
$ans ^= $A[$i];
return $ans;
}
// Driver Code
$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";
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript Program to find the number
// whose XOR sum with given array is
// equal to a given number k
// This function returns the number to
// be inserted in the given array
function findEletobeInserted(A, n, k)
{
// initialise the answer with k
var ans = k;
for(var i = 0; i < n; i++)
// XOR of all elements in
// the array
ans ^= A[i];
return ans;
}
// Driver Code
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);
// This code is contributed by Khushboogoyal499
</script>
Output11 has to be inserted in the given array to make xor sum of 10
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Number whose sum of XOR with given array range is maximum You are given a sequence of N integers and Q queries. In each query, you are given two parameters L and R. You have to find the smallest integer X such that 0 <= X < 2^31 and the sum of XOR of x with all elements in range [L, R] is maximum possible.Examples : Input : A = {20, 11, 18, 2, 13} Th
15+ min read
Find a number which give minimum sum when XOR with every number of array of integers Given an array arr[] of non-negative integers, the task is to find an integer X such that (arr[0] XOR X) + (arr[1] XOR X) + ... + arr[n - 1] XOR X is minimum possible.Examples: Input: arr[] = {3, 9, 6, 2, 4} Output: X = 2, Sum = 22Input: arr[] = {6, 56, 78, 34} Output: X = 2, Sum = 170 Approach: We
8 min read
XOR of every element of an Array with a given number K Given an array arr and a number K, find the new array formed by performing XOR of the corresponding element from the given array with the given number K.Examples: Input: arr[] = { 2, 4, 1, 3, 5 }, K = 5 Output: 7 1 4 6 0 Explanation: 2 XOR 5 = 7 4 XOR 5 = 1 1 XOR 5 = 4 3 XOR 5 = 6 5 XOR 5 = 0 Input:
5 min read
XOR of numbers that appeared even number of times in given Range Given an array of numbers of size N and Q queries. Each query or a range can be represented by L (LeftIndex) and R(RightIndex). Find the XOR-sum of the numbers that appeared even number of times in the given range. Prerequisite : Queries for number of distinct numbers in given range. | Segment Tree
15+ min read
Construct the smallest possible Array with given Sum and XOR Given two positive integers S and X which represents the sum and Bitwise XOR of all the elements of an array arr[]. The task is to find the elements of the array arr[]. If no such array can be generated, print -1.Examples: Input: Sum = 4, Xor = 2 Output: {3, 1} Explanation: Sum of 1 and 3 is 4. Bitw
7 min read
Minimize K whose XOR with given array elements leaves array unchanged Given an array of N elements, the task is to find the minimum value of K such that Bitwise XOR of K with all array elements produces the same set of elements. If it is impossible to find any value of K then print "-1". Examples: Input: arr[] = { 1, 0, 2, 3} Output: 1 Explanation: For K = 1, 1 xor 1
7 min read