数组的最小大小,MEX 为 A,数组元素的 XOR 为 B
给定两个整数A和B ,任务是找到数组的MEX的最小可能大小 是A和按位异或 所有数组元素中的B 。
例子:
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
方法:给定的问题可以通过以下观察来解决:
- 由于数组的 MEX 应为A ,因为必须包含[0, A – 1]范围内的所有元素。
- 为了使所有数组元素的异或为B ,必须在数组中添加几个整数,可以分为 3 种情况。假设变量currXOR在[0, A – 1]范围内存储 XOR 的值,可以使用本文中讨论的方法计算该值。
- 案例 1,其中currXor = B 。在这种情况下,不需要添加整数。
- 案例 2,其中currXor^B = A 。在这种情况下,由于A不能添加到数组中,因此必须添加2 个整数,使得它们的XOR为A 。
- 在所有其他情况下,必须添加1 个整数才能使给定数组的XOR为B 。
因此,上述问题可以通过对从0到A-1的所有数字进行异或并检查上述情况来解决。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
// 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
Python3
# 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#
// 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
Javascript
输出:
2
时间复杂度: O(1)
辅助空间: O(1)