给定一个由N个非零正整数和一个整数K 组成的数组arr[] ,任务是找到K 个最大素数和合数的异或。
例子:
Input: arr[] = {4, 2, 12, 13, 5, 19}, K = 3
Output:
Prime XOR = 27
Composite XOR = 8
5, 13 and 19 are the three maximum primes
from the given array and 5 ^ 13 ^ 19 = 27.
There are only 2 composites in the array i.e. 4 and 12.
And 4 ^ 12 = 8
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}, K = 1
Output:
Prime XOR = 7
Composite XOR = 6
方法:使用埃拉托色尼筛法生成一个布尔向量,其大小为数组中最大元素的大小,可用于检查数字是否为素数。
现在遍历数组,并插入所有这些都在最大堆maxHeapPrime和最大堆maxHeapNonPrime所有的合数质的数字。
现在,从两个最大堆中弹出前K 个元素并取这些元素的异或。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function for Sieve of Eratosthenes
vector SieveOfEratosthenes(int max_val)
{
// Create a boolean vector "prime[0..n]". A
// value in prime[i] will finally be false
// if i is Not a prime, else true.
vector prime(max_val + 1, true);
// Set 0 and 1 as non-primes as
// they don't need to be
// counted as prime numbers
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= max_val; p++) {
// If prime[p] is not changed, then
// it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= max_val; i += p)
prime[i] = false;
}
}
return prime;
}
// Function that calculates the xor
// of k smallest and k
// largest prime numbers in an array
void kMaxXOR(int arr[], int n, int k)
{
// Find maximum value in the array
int max_val = *max_element(arr, arr + n);
// Use sieve to find all prime numbers
// less than or equal to max_val
vector prime = SieveOfEratosthenes(max_val);
// Min Heaps to store the max K prime
// and composite numbers
priority_queue, greater >
minHeapPrime, minHeapNonPrime;
for (int i = 0; i < n; i++) {
// If current element is prime
if (prime[arr[i]]) {
// Min heap will only store k elements
if (minHeapPrime.size() < k)
minHeapPrime.push(arr[i]);
// If the size of min heap is K and the
// top element is smaller than the current
// element than it needs to be replaced
// by the current element as only
// max k elements are required
else if (minHeapPrime.top() < arr[i]) {
minHeapPrime.pop();
minHeapPrime.push(arr[i]);
}
}
// If current element is composite
else if (arr[i] != 1) {
// Heap will only store k elements
if (minHeapNonPrime.size() < k)
minHeapNonPrime.push(arr[i]);
// If the size of min heap is K and the
// top element is smaller than the current
// element than it needs to be replaced
// by the current element as only
// max k elements are required
else if (minHeapNonPrime.top() < arr[i]) {
minHeapNonPrime.pop();
minHeapNonPrime.push(arr[i]);
}
}
}
long long int primeXOR = 0, nonPrimeXor = 0;
while (k--) {
// Calculate the xor
if (minHeapPrime.size() > 0) {
primeXOR ^= minHeapPrime.top();
minHeapPrime.pop();
}
if (minHeapNonPrime.size() > 0) {
nonPrimeXor ^= minHeapNonPrime.top();
minHeapNonPrime.pop();
}
}
cout << "Prime XOR = " << primeXOR << "\n";
cout << "Composite XOR = " << nonPrimeXor << "\n";
}
// Driver code
int main()
{
int arr[] = { 4, 2, 12, 13, 5, 19 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
kMaxXOR(arr, n, k);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function for Sieve of Eratosthenes
static boolean[] SieveOfEratosThenes(int max_val)
{
// Create a boolean vector "prime[0..n]". A
// value in prime[i] will finally be false
// if i is Not a prime, else true.
boolean[] prime = new boolean[max_val + 1];
Arrays.fill(prime, true);
// Set 0 and 1 as non-primes as
// they don't need to be
// counted as prime numbers
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= max_val; p++)
{
// If prime[p] is not changed, then
// it is a prime
if (prime[p])
{
// Update all multiples of p
for (int i = p * 2; i <= max_val; i += p)
prime[i] = false;
}
}
return prime;
}
// Function that calculates the xor
// of k smallest and k
// largest prime numbers in an array
static void kMinXOR(Integer[] arr, int n, int k)
{
// Find maximum value in the array
int max_val = Collections.max(Arrays.asList(arr));
// Use sieve to find all prime numbers
// less than or equal to max_val
boolean[] prime = SieveOfEratosThenes(max_val);
// Min Heaps to store the max K prime
// and composite numbers
PriorityQueue minHeapPrime = new PriorityQueue<>();
PriorityQueue minHeapNonPrime = new PriorityQueue<>();
for (int i = 0; i < n; i++)
{
// If current element is prime
if (prime[arr[i]])
{
// Min heap will only store k elements
if (minHeapPrime.size() < k)
minHeapPrime.add(arr[i]);
// If the size of min heap is K and the
// top element is smaller than the current
// element than it needs to be replaced
// by the current element as only
// max k elements are required
else if (minHeapPrime.peek() < arr[i])
{
minHeapPrime.poll();
minHeapPrime.add(arr[i]);
}
}
// If current element is composite
else if (arr[i] != -1)
{
// Heap will only store k elements
if (minHeapNonPrime.size() < k)
minHeapNonPrime.add(arr[i]);
// If the size of min heap is K and the
// top element is smaller than the current
// element than it needs to be replaced
// by the current element as only
// max k elements are required
else if (minHeapNonPrime.peek() < arr[i])
{
minHeapNonPrime.poll();
minHeapNonPrime.add(arr[i]);
}
}
}
long primeXOR = 0, nonPrimeXor = 0;
while (k-- > 0)
{
// Calculate the xor
if (minHeapPrime.size() > 0)
{
primeXOR ^= minHeapPrime.peek();
minHeapPrime.poll();
}
if (minHeapNonPrime.size() > 0)
{
nonPrimeXor ^= minHeapNonPrime.peek();
minHeapNonPrime.poll();
}
}
System.out.println("Prime XOR = " + primeXOR);
System.out.println("Composite XOR = " + nonPrimeXor);
}
// Driver Code
public static void main(String[] args)
{
Integer[] arr = { 4, 2, 12, 13, 5, 19 };
int n = arr.length;
int k = 3;
kMinXOR(arr, n, k);
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python implimentation of above approach
import heapq
# Function for Sieve of Eratosthenes
def SieveOfEratosthenes(max_val: int) -> list:
# Create a boolean vector "prime[0..n]". A
# value in prime[i] will finally be false
# if i is Not a prime, else true.
prime = [True] * (max_val + 1)
# Set 0 and 1 as non-primes as
# they don't need to be
# counted as prime numbers
prime[0] = False
prime[1] = False
p = 2
while p * p <= max_val:
# If prime[p] is not changed, then
# it is a prime
if prime[p]:
# Update all multiples of p
for i in range(p * 2, max_val + 1, p):
prime[i] = False
p += 1
return prime
# Function that calculates the xor
# of k smallest and k
# largest prime numbers in an array
def kMaxXOR(arr: list, n: int, k: int):
# Find maximum value in the array
max_val = max(arr)
# Use sieve to find all prime numbers
# less than or equal to max_val
prime = SieveOfEratosthenes(max_val)
# Min Heaps to store the max K prime
# and composite numbers
minHeapPrime, minHeapNonPrime = [], []
heapq.heapify(minHeapPrime)
heapq.heapify(minHeapNonPrime)
for i in range(n):
# If current element is prime
if prime[arr[i]]:
# Min heap will only store k elements
if len(minHeapPrime) < k:
heapq.heappush(minHeapPrime, arr[i])
# If the size of min heap is K and the
# top element is smaller than the current
# element than it needs to be replaced
# by the current element as only
# max k elements are required
elif heapq.nsmallest(1, minHeapPrime)[0] < arr[i]:
heapq.heappop(minHeapPrime)
heapq.heappush(minHeapPrime, arr[i])
# If current element is composite
elif arr[i] != 1:
# Heap will only store k elements
if len(minHeapNonPrime) < k:
heapq.heappush(minHeapNonPrime, arr[i])
# If the size of min heap is K and the
# top element is smaller than the current
# element than it needs to be replaced
# by the current element as only
# max k elements are required
elif heapq.nsmallest(1, minHeapNonPrime)[0] < arr[i]:
heapq.heappop(minHeapNonPrime)
heapq.heappush(minHeapNonPrime, arr[i])
primeXOR = 0
nonPrimeXor = 0
while k > 0:
# Calculate the xor
if len(minHeapPrime) > 0:
primeXOR ^= heapq.nsmallest(1, minHeapPrime)[0]
heapq.heappop(minHeapPrime)
if len(minHeapNonPrime) > 0:
nonPrimeXor ^= heapq.nsmallest(1, minHeapNonPrime)[0]
heapq.heappop(minHeapNonPrime)
k -= 1
print("Prime XOR =", primeXOR)
print("Composite XOR =", nonPrimeXor)
# Driver Code
if __name__ == "__main__":
arr = [4, 2, 12, 13, 5, 19]
n = len(arr)
k = 3
kMaxXOR(arr, n, k)
# This code is contributed by
# sanjeev2552
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG {
// Function for Sieve of Eratosthenes
static bool[] SieveOfEratosThenes(int max_val)
{
// Create a boolean vector "prime[0..n]". A
// value in prime[i] will finally be false
// if i is Not a prime, else true.
bool[] prime = new bool[max_val + 1];
for(int i = 0; i < max_val + 1; i++)
{
prime[i] = true;
}
// Set 0 and 1 as non-primes as
// they don't need to be
// counted as prime numbers
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= max_val; p++)
{
// If prime[p] is not changed, then
// it is a prime
if (prime[p])
{
// Update all multiples of p
for (int i = p * 2; i <= max_val; i += p)
prime[i] = false;
}
}
return prime;
}
// Function that calculates the xor
// of k smallest and k
// largest prime numbers in an array
static void kMinXOR(int[] arr, int n, int k)
{
// Find maximum value in the array
int max_val = Int32.MinValue;
for(int i = 0; i < arr.Length; i++)
{
max_val = Math.Max(max_val,arr[i]);
}
// Use sieve to find all prime numbers
// less than or equal to max_val
bool[] prime = SieveOfEratosThenes(max_val);
// Min Heaps to store the max K prime
// and composite numbers
List minHeapPrime = new List();
List minHeapNonPrime = new List();
for (int i = 0; i < n; i++)
{
// If current element is prime
if (prime[arr[i]])
{
// Min heap will only store k elements
if (minHeapPrime.Count < k)
{
minHeapPrime.Add(arr[i]);
}
// If the size of min heap is K and the
// top element is smaller than the current
// element than it needs to be replaced
// by the current element as only
// max k elements are required
else if (minHeapPrime[0] < arr[i])
{
minHeapPrime.RemoveAt(0);
minHeapPrime.Add(arr[i]);
}
minHeapPrime.Sort();
}
// If current element is composite
else if (arr[i] != -1)
{
// Heap will only store k elements
if (minHeapNonPrime.Count < k)
minHeapNonPrime.Add(arr[i]);
// If the size of min heap is K and the
// top element is smaller than the current
// element than it needs to be replaced
// by the current element as only
// max k elements are required
else if (minHeapNonPrime[0] < arr[i])
{
minHeapNonPrime.RemoveAt(0);
minHeapNonPrime.Add(arr[i]);
}
minHeapNonPrime.Sort();
}
}
long primeXOR = 0, nonPrimeXor = 0;
while (k-- > 0)
{
// Calculate the xor
if (minHeapPrime.Count > 0)
{
primeXOR ^= minHeapPrime[0];
minHeapPrime.RemoveAt(0);
}
if (minHeapNonPrime.Count > 0)
{
nonPrimeXor ^= minHeapNonPrime[0];
minHeapNonPrime.RemoveAt(0);
}
}
Console.WriteLine("Prime XOR = " + primeXOR);
Console.WriteLine("Composite XOR = " + nonPrimeXor);
}
// Driver code
static void Main()
{
int[] arr = { 4, 2, 12, 13, 5, 19 };
int n = arr.Length;
int k = 3;
kMinXOR(arr, n, k);
}
}
// This code is contributed by divyesh072019.
Javascript
输出:
Prime XOR = 27
Composite XOR = 8
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。