# XOR of all elements of array with set bits equal to K

Given an array of integers and a number K. The task is to find the XOR of only those elements of the array whose total set bits are equal to K.

**Examples**:

Input: arr[] = {1, 22, 3, 10}, K=1Output: 1 Elements with set bits equal to 1 is 1. So, XOR is also 1.Input: arr[] = {3, 4, 10, 5, 8}, K=2Output: 12

**Approach:**

- Initialize an empty vector.
- Traverse the array form left to right and check the set bits of each element.
- Use, C++ inbuilt function __builtin_popcount() to count setbits.
- Push the elements with K setbits into the vector.
- Finally find XOR of all the elements of the vector.

Below is the implementation of the above approach:

## C++

`// C++ program to find Xor ` `// of all elements with set bits ` `// equal to K ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find Xor ` `// of desired elements ` `int` `xorGivenSetBits(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Initialize vector ` ` ` `vector<` `int` `> v; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(__builtin_popcount(arr[i]) == k) { ` ` ` `// push required elements ` ` ` `v.push_back(arr[i]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Initialize result with first element of vector ` ` ` `int` `result = v[0]; ` ` ` ` ` `for` `(` `int` `i = 1; i < v.size(); i++) ` ` ` `result = result ^ v[i]; ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 13, 1, 19, 7 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 3; ` ` ` ` ` `cout << xorGivenSetBits(arr, n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python 3 program to find Xor of all

# elements with set bits equal to K

# Function to find Xor of desired elements

def xorGivenSetBits(arr, n, k):

# Initialize vector

v = []

for i in range(0, n, 1):

if (bin(arr[i]).count(‘1’) == k):

# push required elements

v.append(arr[i])

# Initialize result with first

# element of vector

result = v[0]

for i in range(1, len(v), 1):

result = result ^ v[i]

return result

# Driver code

if __name__ == ‘__main__’:

arr = [2, 13, 1, 19, 7]

n = len(arr)

k = 3

print(xorGivenSetBits(arr, n, k))

# This code is contributed by

# Surendra_Gangwar

**Output:**

25

## Recommended Posts:

- Sorting array elements with set bits equal to K
- Count pairs in an array such that both elements has equal set bits
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- For each element in 1st array count elements less than or equal to it in 2nd array
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Make all elements of an array equal with the given operation
- Minimum cost to equal all elements of array using two operation
- Count of index pairs with equal elements in an array
- Minimum operation to make all elements equal in array
- Check if the array has an element which is equal to sum of all the remaining elements
- Make all array elements equal with minimum cost
- Adding elements of an array until every element becomes greater than or equal to k
- Sum of elements in an array with frequencies greater than or equal to that element
- Count elements less than or equal to a given value in a sorted rotated array
- Count of smaller or equal elements in sorted array

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.