# 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 = 1

1 xor 0 = 0

1 xor 2 = 2

1 xor 3 = 3

Thus, K = 1 is the least possible positive value which leaves the array unaltered.

Input:arr[] = { 7, 1, 2, 3, 8}

Output:-1

**Naive Approach:** The naive approach is to iterate for all the possible value of **K** in the range **[1, 1024]** and check if Bitwise XOR of **K** with all the elements in the array gives the same array elements or not. If for any minimum value of **K** the Bitwise XOR produces the same array then print that value of **K** else print **“-1”**.

**Time Complexity:** *O(K*N ^{2})*

**Auxiliary Space:**

*O(1)*

**Efficient Approach:** The above approach can be optimized by using additional space. Below are the steps:

- Insert all the elements into the set.
- Iterate for all possible value of
**K**in the range**[0, 1024]**. - For every element in set, find its Bitwise XOR with
**K**. - The first value of
**K**for which all the elements generated after Bitwise XOR with the element in set is same as that of the given set, then print the value of**K**. - If no such
**K**is obtained, print**“-1”**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum ` `// value of K in given range ` `int` `min_value(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `int` `x, X, K; ` ` ` ` ` `// Declare a set ` ` ` `set<` `int` `> S; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `S.insert(arr[i]); ` ` ` `} ` ` ` ` ` `// Initialize count variable ` ` ` `int` `count = 0; ` ` ` ` ` `// Iterate in range [1, 1024] ` ` ` `for` `(` `int` `i = 1; i <= 1024; i++) { ` ` ` ` ` `// counter set as 0 ` ` ` `count = 0; ` ` ` ` ` `// Iterating through the Set ` ` ` `for` `(` `auto` `it = S.begin(); ` ` ` `it != S.end(); it++) ` ` ` ` ` `// Check if the XOR ` ` ` `// calculated is present ` ` ` `// in the Set ` ` ` `{ ` ` ` ` ` `X = ((i | *it) - (i & *it)); ` ` ` ` ` `// If the value of Bitwise XOR ` ` ` `// inside the given set then ` ` ` `// increment count ` ` ` `if` `(S.find(X) != S.end()) { ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Check if the value of count is ` ` ` `// equal to the size of set ` ` ` `if` `(count == S.size()) { ` ` ` `K = i; ` ` ` ` ` `// Return minimum value of K ` ` ` `return` `K; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If no such K is found ` ` ` `return` `-1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given array ` ` ` `int` `arr[] = { 1, 0, 3, 3, 0, 2 }; ` ` ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// Function Call ` ` ` `cout << min_value(arr, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** *O(K*N*log _{2}N)*

**Auxiliary Space:**

*O(1)*

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimize the non-zero elements in the Array by given operation
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Minimize the maximum difference between adjacent elements in an array
- Reverse a subarray of the given array to minimize the sum of elements at even position
- Rearrange an array to minimize sum of product of consecutive pair elements
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Minimize the cost to make all the adjacent elements distinct in an Array
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Find original array from encrypted array (An array of sums of other elements)
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Check if Array elements can be maximized upto M by adding all elements from another array
- Minimize the sum of the array according the given condition
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Remove an element to minimize the LCM of the given array
- Minimize the cost of partitioning an array into K groups
- Minimize sum of adjacent difference with removal of one element from array
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimize insertions in an array to obtain all sums upto N
- Minimize the number of steps required to reach the end of the array
- Minimize the maximum minimum difference after one removal from 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.