Generate an array of minimum sum whose XOR of same-indexed elements with given array are Prime Numbers
Last Updated :
15 Apr, 2021
Given an array Arr[] of N ( 1 ? N ? 105)integers, the task is to generate an array B[] consisting of N non-zero elements, such that XOR of Ai ^ Bi always results in a prime number.
Note: The sum of XORs obtained should be minimized.
Examples:
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}
Approach: This problem can be solved using a Greedy technique. Follow the steps below to solve the problem:
- Since 2 is the smallest prime number possible, XOR of Arr[i] with B[i] = (Arr[i] ^ 2) will give us a prime number 2.
- The contradiction arises when any of the array elements itself is Arr[i] = 2. In this case, B[i] = 2 ^ 2 results in 0.
- Therefore, if Arr[i] = 2, set B[i] = (2 ^ 3) = 1, such that Arr[i] ^ K = 3, next smallest prime number.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
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<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
cout << (Arr[i] ^ 3) << " ";
}
// Otherwise
else {
// Print its XOR with 2
cout << (Arr[i] ^ 2) << " ";
}
}
}
// Driver Code
int main()
{
// Given array
vector<int> 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
<script>
// JavaScript implementation of the above approach
// Function to generate an array whose XOR
// with same-indexed elements of the given
// array is always a prime
function minXOR(Arr, N)
{
// Traverse the array
for(let i = 0; i < N; i++)
{
// If current array element is 2
if (Arr[i] == 2)
{
// Print its XOR with 3
document.write((Arr[i] ^ 3) + " ");
}
// Otherwise
else
{
// Print its XOR with 2
document.write((Arr[i] ^ 2) + " ");
}
}
}
// Driver code
// Given array
let Arr = [ 5, 4, 7, 6 ];
// Size of the array
let N = Arr.length;
// Prints the required array
minXOR(Arr, N);
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Insert minimum number in array so that sum of array becomes prime Given an array of n integers. Find minimum number to be inserted in array, so that sum of all elements of array becomes prime. If sum is already prime, then return 0. Examples : Input : arr[] = { 2, 4, 6, 8, 12 } Output : 5 Input : arr[] = { 3, 5, 7 } Output : 0Recommended PracticeTransform to prime
15+ min read
Find a number which give minimum sum when XOR with every number of array of integers Given an array arr[] of non-negative integers, the task is to find an integer X such that (arr[0] XOR X) + (arr[1] XOR X) + ... + arr[n - 1] XOR X is minimum possible.Examples: Input: arr[] = {3, 9, 6, 2, 4} Output: X = 2, Sum = 22Input: arr[] = {6, 56, 78, 34} Output: X = 2, Sum = 170 Approach: We
8 min read
Number whose sum of XOR with given array range is maximum You are given a sequence of N integers and Q queries. In each query, you are given two parameters L and R. You have to find the smallest integer X such that 0 <= X < 2^31 and the sum of XOR of x with all elements in range [L, R] is maximum possible.Examples : Input : A = {20, 11, 18, 2, 13} Th
15+ min read
Minimize K whose XOR with given array elements leaves array unchanged Given an array of N elements, the task is to find the minimum value of K such that Bitwise XOR of K with all array elements produces the same set of elements. If it is impossible to find any value of K then print "-1". Examples: Input: arr[] = { 1, 0, 2, 3} Output: 1 Explanation: For K = 1, 1 xor 1
7 min read
Minimize Bitwise XOR of array elements with 1 required to make sum of array at least K Given an array arr[] consisting of N positive integers and a positive integer K, the task is to count minimum Bitwise XOR of array elements with 1 required such that the sum of the array is at least K. Examples: Input: arr[] = {0, 1, 1, 0, 1}, K = 4Output: 1Explanation: Performing Bitwise XOR of arr
6 min read
Nearest prime number in the array of every array element Given an integer array arr[] consisting of N integers, the task is to find the nearest Prime Number in the array for every element in the array. If the array does not contain any prime number, then print -1. Examples: Input: arr[] = {1, 2, 3, 1, 6} Output: 2 2 3 3 3 Explanation: For the subarray {1,
11 min read