MEX of generated sequence of N+1 integers where ith integer is XOR of (i-1) and K
Given two integers N and K, generate a sequence of size N+1 where the ith element is (i-1)⊕K, the task is to find the MEX of this sequence. Here, the MEX of a sequence is the smallest non-negative integer that does not occur in the sequence.
Examples:
Input: N = 7, K=3
Output: 8
Explanation: Sequence formed by given N and K is {0⊕3, 1⊕3, 2⊕3, 3⊕3, 4⊕3, 5⊕3, 6⊕3, 7⊕3} i.e {3, 2, 1, 0, 7, 6, 5, 4}
Smallest non-negative number not present in the given sequence is 8
Input: N = 6, K=4
Output: 3
Native Approach: The simplest approach to solve this problem is to simply make the array of the given and calculate its MEX. Following are the steps to solve this problem:
- Generate the sequence and sort it.
- Initialize MEX to 0 and iterate over the sorted array, if the current element is equal to MEX increase MEX by 1 otherwise break the loop.
- Print MEX as the answer to this problem.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMex( int N, int K)
{
int A[N + 1];
for ( int i = 0; i <= N; i++) {
A[i] = i ^ K;
}
sort(A, A + N + 1);
int MEX = 0;
for ( int x : A) {
if (x == MEX) {
MEX++;
}
else {
break ;
}
}
return MEX;
}
int main()
{
int N = 7, K = 3;
cout << findMex(N, K);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int findMex( int N, int K)
{
int [] A = new int [N + 1 ];
for ( int i = 0 ; i <= N; i++)
{
A[i] = i ^ K;
}
Arrays.sort(A);
int MEX = 0 ;
for ( int i = 0 ; i < A.length; i++)
{
if (A[i] == MEX)
{
MEX++;
}
else
{
break ;
}
}
return MEX;
}
public static void main(String args[])
{
int N = 7 , K = 3 ;
System.out.println(findMex(N, K));
}
}
|
Python3
def findMex(N, K):
A = [ 0 ] * (N + 1 )
for i in range (N + 1 ):
A[i] = i ^ K
A.sort()
MEX = 0
for x in A:
if (x = = MEX):
MEX + = 1
else :
break
return MEX
N = 7
K = 3
print (findMex(N, K))
|
C#
using System;
class GFG{
static int findMex( int N, int K)
{
int [] A = new int [N + 1];
for ( int i = 0; i <= N; i++)
{
A[i] = i ^ K;
}
Array.Sort(A);
int MEX = 0;
foreach ( int x in A)
{
if (x == MEX)
{
MEX++;
}
else
{
break ;
}
}
return MEX;
}
public static void Main()
{
int N = 7, K = 3;
Console.WriteLine(findMex(N, K));
}
}
|
Javascript
<script>
function findMex(N, K) {
let A = new Array(N + 1);
for (let i = 0; i <= N; i++) {
A[i] = i ^ K;
}
A.sort();
let MEX = 0;
for (x of A) {
if (x == MEX) {
MEX++;
}
else {
break ;
}
}
return MEX;
}
let N = 7
let K = 3;
document.write(findMex(N, K))
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Efficient Approach: If a number M is occurring in this sequence then for some ith term (i-1)⊕K = M, which also means M⊕K = (i-1), where the maximum value of (i-1) is N. Now, assume that X is the MEX of the given sequence, then X⊕K must be greater than N, i.e X⊕K > N. So, the minimum value of X is the MEX of the sequence. Follow the below steps, to solve this problem:
- Iterate through the bits of both N+1 and K from backwards.
- If the ith bit of K is equal to the ith bit of N+1, set the ith bit of X to 0.
- If the ith bit of K is equal to 0 and the ith bit of N+1 is equal to 1, set the ith bit of X to 1.
- If the ith bit of K is equal to 1 and the ith bit of N+1 is equal to 0, set all the remaining lower bits of X to 0 because this means that the number which contains a set bit in this position is missing and it is the MEX of the sequence.
- Print the answer according to the above observation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMEX( int N, int K)
{
int lastBit = max(round(log2(N + 1)),
round(log2(K)));
int X = 0;
for ( int i = lastBit; i >= 0; i--) {
if ((K >> i & 1) == ((N + 1)
>> i
& 1))
continue ;
else if ((K >> i & 1) == 0)
X = X | (1 << i);
else
break ;
}
return X;
}
int main()
{
int N = 6, K = 4;
cout << findMEX(N, K);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int log2( int N)
{
int result = ( int )(Math.log(N) / Math.log( 2 ));
return result;
}
static int findMEX( int N, int K)
{
int lastBit = Math.max(Math.round(log2(N + 1 )),
Math.round(log2(K)));
int X = 0 ;
for ( int i = lastBit; i >= 0 ; i--) {
if ((K >> i & 1 ) == ((N + 1 )
>> i & 1 ))
continue ;
else if ((K >> i & 1 ) == 0 )
X = X | ( 1 << i);
else
break ;
}
return X;
}
public static void main(String args[])
{
int N = 6 , K = 4 ;
System.out.println(findMEX(N, K));
}
}
|
Python3
from math import *
def findMEX(N, K):
lastBit = max ( round (log2(N + 1 )), round (log2(K)))
X = 0
for i in range (lastBit, - 1 , - 1 ):
if ((K >> i & 1 ) = = ((N + 1 ) >> i & 1 )):
continue
elif ((K >> i & 1 ) = = 0 ):
X = X | ( 1 << i)
else :
break
return X
N = 6
K = 4
print (findMEX(N, K))
|
C#
using System;
class GFG
{
static double log2( int N)
{
double result = (Math.Log(N) / Math.Log(2));
return result;
}
static int findMEX( int N, int K)
{
int lastBit = Math.Max(( int )Math.Round(log2(N + 1)),
( int )Math.Round(log2(K)));
int X = 0;
for ( int i = lastBit; i >= 0; i--) {
if ((K >> i & 1) == ((N + 1)
>> i & 1))
continue ;
else if ((K >> i & 1) == 0)
X = X | (1 << i);
else
break ;
}
return X;
}
public static void Main()
{
int N = 6, K = 4;
Console.Write(findMEX(N, K));
}
}
|
Javascript
<script>
const findMEX = (N, K) => {
let lastBit = Math.max(Math.round(Math.log2(N + 1)),
Math.round(Math.log2(K)));
let X = 0;
for (let i = lastBit; i >= 0; i--) {
if ((K >> i & 1) == ((N + 1)
>> i
& 1))
continue ;
else if ((K >> i & 1) == 0)
X = X | (1 << i);
else
break ;
}
return X;
}
let N = 6, K = 4;
document.write(findMEX(N, K));
</script>
|
Time Complexity: O(log(max(N, K))
Auxiliary Space: O(1)
Last Updated :
31 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...