Open In App

Minimum size of the array with MEX as A and XOR of the array elements as B

Given two integers A and B, the task is to find the minimum possible size of the array whose MEX of the array is A and Bitwise XOR of all array elements is B.

Examples:



Input: A = 1, B = 1
Output: 3
Explanation: The array which can satisfy the given condition is {0, 3, 2} which is of minimum possible size i.e, 3.

Input: A = 1, B = 999
Output: 2



 

Approach: The given problem can be solved by the following observations:

Therefore, the above problem can be solved by finding the XOR of all numbers from 0 to A-1 and checking for the above-mentioned cases.

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum size
// of array with given MEX and XOR
int minimumSizeArr(int A, int B)
{
    int currXor = 0;
 
    // Find the XOR of values from
    // 0 to A-1
    int reminder = (A - 1) % 4;
 
    // If A is a multiple of 4
    if (reminder == 0)
        currXor = A - 1;
 
    // If A % 4 gives remainder 1
    else if (reminder == 1)
        currXor = 1;
 
    // If A % 4 gives remainder 2
    else if (reminder == 2)
        currXor = A;
 
    // Initializing array size by A
    int minSize = A;
 
    // If XOR of all values of array
    // is equal to B
    if (currXor == B)
        return minSize;
 
    // If the required integer to be
    // added is equal to A
    else if (currXor ^ B == A)
        return minSize + 2;
 
    // Else any integer can be added
    else
        return minSize + 1;
}
 
// Driver Code
int main()
{
    int A = 1;
    int B = 999;
    cout << minimumSizeArr(A, B);
 
    return 0;
}




// Java program for the above approach
public class GFG {
     
    // Function to find the minimum size
    // of array with given MEX and XOR
    static int minimumSizeArr(int A, int B)
    {
        int currXor = 0;
     
        // Find the XOR of values from
        // 0 to A-1
        int reminder = (A - 1) % 4;
     
        // If A is a multiple of 4
        if (reminder == 0)
            currXor = A - 1;
     
        // If A % 4 gives remainder 1
        else if (reminder == 1)
            currXor = 1;
     
        // If A % 4 gives remainder 2
        else if (reminder == 2)
            currXor = A;
     
        // Initializing array size by A
        int minSize = A;
     
        // If XOR of all values of array
        // is equal to B
        if (currXor == B)
            return minSize;
     
        // If the required integer to be
        // added is equal to A
        else if ((currXor ^ B) == A)
            return minSize + 2;
     
        // Else any integer can be added
        else
            return minSize + 1;
    }
     
    // Driver Code
    public static void main (String[] args) {
         
        int A = 1;
        int B = 999;
        System.out.println(minimumSizeArr(A, B));       
    }
}
 
// This code is contributed by AnkThon




# python program for the above approach
 
# Function to find the minimum size
# of array with given MEX and XOR
def minimumSizeArr(A, B):
 
    currXor = 0
 
    # Find the XOR of values from
    # 0 to A-1
    reminder = (A - 1) % 4
 
    # If A is a multiple of 4
    if (reminder == 0):
        currXor = A - 1
 
    # If A % 4 gives remainder 1
    elif (reminder == 1):
        currXor = 1
 
    # If A % 4 gives remainder 2
    elif (reminder == 2):
        currXor = A
 
    # Initializing array size by A
    minSize = A
 
    # If XOR of all values of array
    # is equal to B
    if (currXor == B):
        return minSize
 
    # If the required integer to be
    # added is equal to A
    elif (currXor ^ B == A):
        return minSize + 2
 
    # Else any integer can be added
    else:
        return minSize + 1
 
# Driver Code
if __name__ == "__main__":
 
    A = 1
    B = 999
    print(minimumSizeArr(A, B))
 
# This code is contributed by rakeshsahni




// C# program for the above approach
using System;
public class GFG {
     
    // Function to find the minimum size
    // of array with given MEX and XOR
    static int minimumSizeArr(int A, int B)
    {
        int currXor = 0;
     
        // Find the XOR of values from
        // 0 to A-1
        int reminder = (A - 1) % 4;
     
        // If A is a multiple of 4
        if (reminder == 0)
            currXor = A - 1;
     
        // If A % 4 gives remainder 1
        else if (reminder == 1)
            currXor = 1;
     
        // If A % 4 gives remainder 2
        else if (reminder == 2)
            currXor = A;
     
        // Initializing array size by A
        int minSize = A;
     
        // If XOR of all values of array
        // is equal to B
        if (currXor == B)
            return minSize;
     
        // If the required integer to be
        // added is equal to A
        else if ((currXor ^ B) == A)
            return minSize + 2;
     
        // Else any integer can be added
        else
            return minSize + 1;
    }
     
    // Driver Code
    public static void Main () {
         
        int A = 1;
        int B = 999;
        Console.WriteLine(minimumSizeArr(A, B));       
    }
}
 
// This code is contributed by ihritik




<script>
// Javascript program for the above approach
 
// Function to find the minimum size
// of array with given MEX and XOR
function minimumSizeArr(A, B)
{
  let currXor = 0;
 
  // Find the XOR of values from
  // 0 to A-1
  let reminder = (A - 1) % 4;
 
  // If A is a multiple of 4
  if (reminder == 0) currXor = A - 1;
   
  // If A % 4 gives remainder 1
  else if (reminder == 1) currXor = 1;
   
  // If A % 4 gives remainder 2
  else if (reminder == 2) currXor = A;
 
  // Initializing array size by A
  let minSize = A;
 
  // If XOR of all values of array
  // is equal to B
  if (currXor == B) return minSize;
  // If the required integer to be
  // added is equal to A
  else if (currXor ^ (B == A)) return minSize + 2;
  // Else any integer can be added
  else return minSize + 1;
}
 
// Driver Code
 
let A = 1;
let B = 999;
document.write(minimumSizeArr(A, B));
 
// This code is contributed by saurabh_jaiswal.
</script>

Output
2

Time Complexity: O(1)
Auxiliary Space: O(1)


Article Tags :