- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

To solve a problem in which, given, we are tasked to find the number such that the XOR sum of a given array with that number becomes equal to k, for example.

Input: arr[] = {1, 2, 3, 4, 5}, k = 10 Output: 11 Explanation: 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 11 = 10 Input: arr[] = { 12, 23, 34, 56, 78 }, k = 6 Output: 73

In this program, we are going to use the property of xor if A^B = C and A^C = B, and we are going to apply this in this problem.

In this approach, we will use the above property of the xor operator now. For this problem, now we traverse through the array, and then if we xor the number with k, that will be our answer.

**C++ Code for the Above Approach**

#include <bits/stdc++.h> using namespace std; int main(){ int arr[] = { 1, 2, 3, 4, 5 }; // given array int n = sizeof(arr) / sizeof(int); // size of our array int k = 10; // given k int answer = 0; for(int i = 0; i < n; i++) // traversing the array for // xor sum answer ^= arr[i]; answer ^= k; // XORing with k to get our answer cout << answer << "\n"; // printing our answer return 0; }

11

In this approach, we are going to use some property of xor operator, so for that, we are simply going to traverse through the array and then find the xor sum of the whole array, and then we xor that xor sum with k and that answer and then we print our answer.

In this tutorial, we solve finding the Number whose XOR sum with a given array is a given number k. We also learned the C++ program for this problem and the complete approach (Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.

- Related Questions & Answers
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- Find a value whose XOR with given number is maximum in C++
- Program to find number of sublists whose sum is given target in python
- How do you find continuous sub array whose sum is equal to a given number in Java?
- Minimum number of squares whose sum equals to given number n
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- Program to find array of length k from given array whose unfairness is minimum in python
- Find the Minimum Number of Fibonacci Numbers Whose Sum Is K in C++
- Program to count number of paths whose sum is k in python
- Find smallest number n such that n XOR n+1 equals to given k in C++
- Count number of distinct pairs whose sum exists in the given array in C++
- C++ Program to Sum the digits of a given number
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- C program to find if the given number is perfect number or not
- Find the Number of Unique Triplets Whose XOR is Zero using C++

Advertisements