给定一个由N ( 1 ≤ N ≤ 10 5 ) 个整数组成的数组Arr[] ,任务是生成一个由N 个非零元素组成的数组B[] ,使得A i ^ B i 的异或总是产生一个素数数字。
注意:获得的 XOR 的总和应该最小化。
例子:
Input: arr[] = {5, 4, 7, 6}
Output: {7, 6, 5, 4}
Explanation:
2 is the smallest prime number. Therefore, XORing A[i] with (A[i] ^ 2)
gives us the smallest number which is prime.
A[i] ^ (A[i] ^ 2) = (A[i] ^ A[i]) ^ 2 = 0 ^ 2 = 2
because
1. XOR of 5 ^ 7 = 2, which is prime
2. XOR of 4 ^ 6 = 2, which is prime.
3. XOR of 7 ^ 5 = 2, which is prime.
4. XOR of 6 ^ 4 = 2, which is prime.
The resultant sum is – 2 + 2 + 2 + 2 = 8, which is the minimum possible
Input: arr[] = {10, 16}
Output: {8, 18}
方法:这个问题可以使用贪心技术来解决。请按照以下步骤解决问题:
- 由于 2 是可能的最小素数,因此Arr[i]与B[i] = (Arr[i] ^ 2) 的异或将给我们一个素数2 。
- 当任何数组元素本身是Arr[i] = 2时,就会出现矛盾。在这种情况下, B[i] = 2 ^ 2结果为0 。
- 因此,如果Arr[i] = 2 ,则设置B[i] = (2 ^ 3) = 1 ,使得Arr[i] ^ K = 3 ,下一个最小的素数。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to generate an array whose XOR
// with same-indexed elements of the given
// array is always a prime
void minXOR(vector& Arr, int N)
{
// Traverse the array
for (int i = 0; i < N; i++) {
// If current array element is 2
if (Arr[i] == 2) {
// Print its XOR with 3
cout << (Arr[i] ^ 3) << " ";
}
// Otherwise
else {
// Print its XOR with 2
cout << (Arr[i] ^ 2) << " ";
}
}
}
// Driver Code
int main()
{
// Given array
vector Arr = { 5, 4, 7, 6 };
// Size of the array
int N = Arr.size();
// Prints the required array
minXOR(Arr, N);
return 0;
}
Java
// Java implementation of the above approach
class GFG{
// Function to generate an array whose XOR
// with same-indexed elements of the given
// array is always a prime
private static void minXOR(int Arr[], int N)
{
// Traverse the array
for(int i = 0; i < N; i++)
{
// If current array element is 2
if (Arr[i] == 2)
{
// Print its XOR with 3
System.out.print((Arr[i] ^ 3) + " ");
}
// Otherwise
else
{
// Print its XOR with 2
System.out.print((Arr[i] ^ 2) + " ");
}
}
}
// Driver code
public static void main(String[] args)
{
// Given array
int Arr[] = { 5, 4, 7, 6 };
// Size of the array
int N = Arr.length;
// Prints the required array
minXOR(Arr, N);
}
}
// This code is contributed by MuskanKalra1
Python3
# Python3 implementation of the above approach
# Function to generate an array whose XOR
# with same-indexed elements of the given
# array is always a prime
def minXOR(Arr, N):
# Traverse the array
for i in range(N):
# If current array element is 2
if (Arr[i] == 2):
# Print its XOR with 3
print(Arr[i] ^ 3,end=" ")
# Otherwise
else:
# Print its XOR with 2
print(Arr[i] ^ 2,end=" ")
# Driver Code
if __name__ == '__main__':
# Given array
Arr = [5, 4, 7, 6 ]
# Size of the array
N = len(Arr)
# Prints the required array
minXOR(Arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to generate an array whose XOR
// with same-indexed elements of the given
// array is always a prime
private static void minXOR(int[] Arr, int N)
{
// Traverse the array
for(int i = 0; i < N; i++)
{
// If current array element is 2
if (Arr[i] == 2)
{
// Print its XOR with 3
Console.Write((Arr[i] ^ 3) + " ");
}
// Otherwise
else
{
// Print its XOR with 2
Console.Write((Arr[i] ^ 2) + " ");
}
}
}
// Driver code
public static void Main()
{
// Given array
int[] Arr = { 5, 4, 7, 6 };
// Size of the array
int N = Arr.Length;
// Prints the required array
minXOR(Arr, N);
}
}
Javascript
7 6 5 4
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。