Number whose XOR sum with given array is a given number k

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right



Output :

11 has to be inserted in the given array to make xor sum of 10


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, SURENDRA_GANGWAR



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.