Minimum size of the array with MEX as A and XOR of the array elements as B
Last Updated :
31 Oct, 2023
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:
- As the MEX of the array should be A as all the elements over the range [0, A – 1] must be included.
- In order to make the XOR of all array elements as B, a few integers must be added to the array which can be classified into 3 cases. Suppose variable currXOR stores the value of XOR over the range [0, A – 1] which can be calculated using the approach discussed in this article.
- Case 1 where currXor = B. In this case, no integers are required to be added.
- Case 2 where currXor^B = A. In this case, since A cannot be added to the array, 2 integers must be added such that their XOR is A.
- In all the other cases, 1 integer must be added in order to make the XOR of the given array as B.
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++
#include <bits/stdc++.h>
using namespace std;
int minimumSizeArr( int A, int B)
{
int currXor = 0;
int reminder = (A - 1) % 4;
if (reminder == 0)
currXor = A - 1;
else if (reminder == 1)
currXor = 1;
else if (reminder == 2)
currXor = A;
int minSize = A;
if (currXor == B)
return minSize;
else if (currXor ^ B == A)
return minSize + 2;
else
return minSize + 1;
}
int main()
{
int A = 1;
int B = 999;
cout << minimumSizeArr(A, B);
return 0;
}
|
Java
public class GFG {
static int minimumSizeArr( int A, int B)
{
int currXor = 0 ;
int reminder = (A - 1 ) % 4 ;
if (reminder == 0 )
currXor = A - 1 ;
else if (reminder == 1 )
currXor = 1 ;
else if (reminder == 2 )
currXor = A;
int minSize = A;
if (currXor == B)
return minSize;
else if ((currXor ^ B) == A)
return minSize + 2 ;
else
return minSize + 1 ;
}
public static void main (String[] args) {
int A = 1 ;
int B = 999 ;
System.out.println(minimumSizeArr(A, B));
}
}
|
Python3
def minimumSizeArr(A, B):
currXor = 0
reminder = (A - 1 ) % 4
if (reminder = = 0 ):
currXor = A - 1
elif (reminder = = 1 ):
currXor = 1
elif (reminder = = 2 ):
currXor = A
minSize = A
if (currXor = = B):
return minSize
elif (currXor ^ B = = A):
return minSize + 2
else :
return minSize + 1
if __name__ = = "__main__" :
A = 1
B = 999
print (minimumSizeArr(A, B))
|
C#
using System;
public class GFG {
static int minimumSizeArr( int A, int B)
{
int currXor = 0;
int reminder = (A - 1) % 4;
if (reminder == 0)
currXor = A - 1;
else if (reminder == 1)
currXor = 1;
else if (reminder == 2)
currXor = A;
int minSize = A;
if (currXor == B)
return minSize;
else if ((currXor ^ B) == A)
return minSize + 2;
else
return minSize + 1;
}
public static void Main () {
int A = 1;
int B = 999;
Console.WriteLine(minimumSizeArr(A, B));
}
}
|
Javascript
<script>
function minimumSizeArr(A, B)
{
let currXor = 0;
let reminder = (A - 1) % 4;
if (reminder == 0) currXor = A - 1;
else if (reminder == 1) currXor = 1;
else if (reminder == 2) currXor = A;
let minSize = A;
if (currXor == B) return minSize;
else if (currXor ^ (B == A)) return minSize + 2;
else return minSize + 1;
}
let A = 1;
let B = 999;
document.write(minimumSizeArr(A, B));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...