Minimize K whose XOR with given array elements leaves array unchanged
Last Updated :
12 May, 2021
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 = 0
1 xor 0 = 1
1 xor 2 = 3
1 xor 3 = 2
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 values 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*N2)
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 values of K in the range [0, 1024].
- For every element in the set, find its Bitwise XOR with K.
- The first value of K for which all the elements generated after Bitwise XOR with the element inset is the 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++
#include <bits/stdc++.h>
using namespace std;
int min_value( int arr[], int N)
{
int x, X, K;
set< int > S;
for ( int i = 0; i < N; i++) {
S.insert(arr[i]);
}
int count = 0;
for ( int i = 1; i <= 1024; i++) {
count = 0;
for ( auto it = S.begin(); it != S.end(); it++)
{
X = ((i | *it) - (i & *it));
if (S.find(X) != S.end()) {
count++;
}
}
if (count == S.size()) {
K = i;
return K;
}
}
return -1;
}
int main()
{
int arr[] = { 1, 0, 3, 3, 0, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << min_value(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int min_value( int arr[], int N)
{
int x, X, K;
HashSet<Integer> S = new HashSet<Integer>();
for ( int i = 0 ; i < N; i++) {
S.add(arr[i]);
}
int count = 0 ;
for ( int i = 1 ; i <= 1024 ; i++) {
count = 0 ;
for ( int it : S) {
X = ((i | it) - (i & it));
if (S.contains(X)) {
count++;
}
}
if (count == S.size()) {
K = i;
return K;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 0 , 3 , 3 , 0 , 2 };
int N = arr.length;
System.out.print(min_value(arr, N));
}
}
|
Python3
def min_value(arr, N):
x, X, K = 0 , 0 , 0
S = set ()
for i in range (N):
S.add(arr[i])
count = 0
for i in range ( 1 , 1024 ):
count = 0
for it in S:
X = ((i | it) - (i & it))
if X in S:
count + = 1
if (count = = len (S)):
K = i
return K
return - 1
arr = [ 1 , 0 , 3 , 3 , 0 , 2 ]
N = len (arr)
print (min_value(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int min_value( int [] arr, int N)
{
int X, K;
HashSet< int > S = new HashSet< int >();
for ( int i = 0; i < N; i++) {
S.Add(arr[i]);
}
int count = 0;
for ( int i = 1; i <= 1024; i++) {
count = 0;
foreach ( int it in S)
{
X = ((i | it) - (i & it));
if (S.Contains(X)) {
count++;
}
}
if (count == S.Count) {
K = i;
return K;
}
}
return -1;
}
static void Main()
{
int [] arr = { 1, 0, 3, 3, 0, 2 };
int N = arr.Length;
Console.Write(min_value(arr, N));
}
}
|
Javascript
<script>
function min_value(arr, N)
{
let x, X, K;
let S = [];
for (let i = 0; i < N; i++) {
S.push(arr[i]);
}
let count = 0;
for (let i = 1; i <= 1024; i++) {
count = 0;
for (let it in S) {
X = ((i | it) - (i & it));
for (let j in S) {
if (S[j]==X) {
count++;
}
}
}
if (count == S.length) {
K = i;
return K;
}
}
return -1;
}
let arr = [ 1, 0, 3, 3, 0, 2 ];
let N = arr.length;
document.write(min_value(arr, N));
</script>
|
Time Complexity: O(K*N*log2N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...