Horje
Find the MEX of given XOR sequence

Given two integers N and M. The task is to determine the MEX (Minimal Excluded) of the following sequence: N ^ 0, N ^ 1, N ^ 2… N ^ M, where ^ is the Bitwise XOR operator.

Examples:

Input: N = 4, M = 2
Output: 0
Explanation: The sequence will be: 4 ^ 0, 4 ^ 1, 4 ^ 2 = 4, 5, 6. So, the MEX will be 0.

Input: N = 5, M = 7
Output:
Explanation: The sequence will be: 5 ^ 0, 5 ^ 1, 5 ^ 2, 5 ^ 3, 5 ^ 4, 5 ^ 5, 5 ^ 6, 5 ^ 7 = 5, 4, 7, 6, 1, 0, 3, 2. So, the MEX will be 8.

Approach: To solve the problem, follow the below idea:

The problem is to find whether a number K is present in the sequence N ^ 0, N ^ 1, N ^ 2 … N ^ M. If K is present in the sequence, then there must be an integer X (X <= M), such that N ^ X = K. We can also write N ^ X = K as N ^ K = X. So, now if we want to find whether a number K is present in the sequence N ^ 0, N ^ 1, N ^ 2 … N ^ M, we can simply check N ^ K <= M.

Now, the problem has been reduced to find the smallest number K such that N ^ K >= M + 1. For simplicity, let’s consider P=M + 1. So, we need to find the smallest K such that N ^ K >= P.

We can build P bit by bit starting from the highest bit and moving towards the lowest bit.

  • If Pi = 1 and Ni = 1, then set Ki = 0, as Ni ^ 0 >= Pi.
  • If Pi = 0 and Ni = 0, then set Ki = 0, as Ni ^ 0 >= Pi.
  • If Pi = 0 and Ni = 1, then set Ki = 0, we can break here by setting the remaining bits of K off as no matter what the remaining bits of N are N ^ K > P.
  • If Pi = 0 and Ni = 1, then set Ki = 1 as we want to have Ni ^ 1 >= Pi.

Step-by-step algorithm:

  • Run a loop from i = 31 to 0 to iterate from Most Significant Bit to Least Significant Bit.
  • Start building K bit by bit according to the above conditions.
  • Finally return K as the final answer.

Below is the implementation of the approach:

C++
#include <bits/stdc++.h>
using namespace std;

int solve(int N, int M) {
    int P = M + 1;
    int K = 0;
    for (int i = 30; i >= 0; i--) {
        int ithBitN = (N >> i & 1);
        int ithBitP = (P >> i & 1);
        if (ithBitN == ithBitP)
            continue;
        if (ithBitN == 1)
            break;
        if (ithBitP == 1) {
            K |= 1 << i;
        }
    }
    return K;
}

int main()
{
    int N = 5, M = 7;
    cout << solve(N, M) << "\n";
    return 0;
}
Java
import java.util.*;

class Main {
    // Function to find the largest value K such that K XOR M is less than N
    static int solve(int N, int M) {
        int P = M + 1;
        int K = 0;
        // Iterate through each bit position from the most significant bit to the least significant bit
        for (int i = 30; i >= 0; i--) {
            int ithBitN = (N >> i) & 1;
            int ithBitP = (P >> i) & 1;
            // Check if the corresponding bits in N and P are different
            if (ithBitN == ithBitP)
                continue;
            // If the bit in N is 1, break the loop
            if (ithBitN == 1)
                break;
            // If the bit in P is 1, set the corresponding bit in K
            if (ithBitP == 1) {
                K |= 1 << i;
            }
        }
        // Return the final value of K
        return K;
    }

    public static void main(String[] args) {
        // Example input values
        int N = 5, M = 7;
        // Print the result of the solve function
        System.out.println(solve(N, M));
    }
}

// This code is contributed by shivamgupta310570
C#
using System;

class MainClass {
    // Function to find the largest value K such that K XOR M is less than N
    static int Solve(int N, int M) {
        int P = M + 1;
        int K = 0;
        // Iterate through each bit position from the most significant bit to the least significant bit
        for (int i = 30; i >= 0; i--) {
            int ithBitN = (N >> i) & 1;
            int ithBitP = (P >> i) & 1;
            // Check if the corresponding bits in N and P are different
            if (ithBitN == ithBitP)
                continue;
            // If the bit in N is 1, break the loop
            if (ithBitN == 1)
                break;
            // If the bit in P is 1, set the corresponding bit in K
            if (ithBitP == 1) {
                K |= 1 << i;
            }
        }
        // Return the final value of K
        return K;
    }

    public static void Main(string[] args) {
        // Example input values
        int N = 5, M = 7;
        // Print the result of the Solve function
        Console.WriteLine(Solve(N, M));
    }
}

// This code is contributed by shivamgupta310570
JavaScript
// Function to solve the problem
function solve(N, M) {
    // Increment M by 1
    let P = M + 1;
    let K = 0;
    // Loop from 30 to 0
    for (let i = 30; i >= 0; i--) {
        // Get the ith bit of N
        let ithBitN = (N >> i) & 1;
        // Get the ith bit of P
        let ithBitP = (P >> i) & 1;
        // If the ith bits of N and P are the same, continue to the next iteration
        if (ithBitN == ithBitP)
            continue;
        // If the ith bit of N is 1, break the loop
        if (ithBitN == 1)
            break;
        // If the ith bit of P is 1, set the ith bit of K
        if (ithBitP == 1) {
            K |= 1 << i;
        }
    }
    // Return the result
    return K;
}

// Main function
function main() {
    let N = 5, M = 7;
    console.log(solve(N, M));
}

// Call the main function
main();
Python3
def solve(N, M):
    # Initialize P with M + 1 and K with 0
    P = M + 1
    K = 0

    # Iterate over each bit position (from 30 to 0)
    for i in range(30, -1, -1):
        # Extract the i-th bit of N and P
        ithBitN = (N >> i) & 1
        ithBitP = (P >> i) & 1

        # If the i-th bits of N and P are equal, continue to the next bit
        if ithBitN == ithBitP:
            continue

        # If the i-th bit of N is 1, break the loop
        if ithBitN == 1:
            break

        # If the i-th bit of P is 1, set the corresponding bit in K
        if ithBitP == 1:
            K |= 1 << i

    return K

def main():
    # Example values for N and M
    N = 5
    M = 7

    # Call the solve function and print the result
    print(solve(N, M))

if __name__ == "__main__":
    # Call the main function if the script is run directly
    main()

Output
8






Time Complexity: O(log (max(N, M))), where N and M are given as input.
Auxiliary Space: O(1)




Reffered: https://www.geeksforgeeks.org


Bit Magic

Related
Find the original number by flipping K unique bits Find the original number by flipping K unique bits
Minimum Increment or Bitwise OR operations to make two integers equal Minimum Increment or Bitwise OR operations to make two integers equal
Bitwise Algorithms Bitwise Algorithms
XOR Folding of a Grid XOR Folding of a Grid
Solving Binary String Modulo Problem Solving Binary String Modulo Problem

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
18