using
System;
using
System.Collections.Generic;
class
GFG
{
static
bool
[] SieveOfEratosThenes(
int
max_val)
{
bool
[] prime =
new
bool
[max_val + 1];
Array.Fill(prime,
true
);
prime[0] =
false
;
prime[1] =
false
;
for
(
int
p = 2; p * p <= max_val; p++)
{
if
(prime[p])
{
for
(
int
i = p * 2; i <= max_val; i += p)
prime[i] =
false
;
}
}
return
prime;
}
static
void
kMinXOR(
int
[] arr,
int
n,
int
k)
{
int
max_val = arr[0];
for
(
int
i = 1; i < arr.Length; i++)
{
max_val = Math.Max(max_val, arr[i]);
}
bool
[] prime = SieveOfEratosThenes(max_val);
List<
int
> maxHeapPrime =
new
List<
int
>();
List<
int
> maxHeapNonPrime =
new
List<
int
>();
for
(
int
i = 0; i < n; i++)
{
if
(prime[arr[i]])
{
if
(maxHeapPrime.Count < k)
{
maxHeapPrime.Add(arr[i]);
maxHeapPrime.Sort();
maxHeapPrime.Reverse();
}
else
if
(maxHeapPrime[0] > arr[i])
{
maxHeapPrime.RemoveAt(0);
maxHeapPrime.Add(arr[i]);
maxHeapPrime.Sort();
maxHeapPrime.Reverse();
}
}
else
if
(arr[i] != -1)
{
if
(maxHeapNonPrime.Count < k)
{
maxHeapNonPrime.Add(arr[i]);
maxHeapNonPrime.Sort();
maxHeapNonPrime.Reverse();
}
else
if
(maxHeapNonPrime[0] > arr[i])
{
maxHeapNonPrime.RemoveAt(0);
maxHeapNonPrime.Add(arr[i]);
maxHeapNonPrime.Sort();
maxHeapNonPrime.Reverse();
}
}
}
long
primeXOR = 0, nonPrimeXor = 0;
while
(k-- > 0)
{
if
(maxHeapPrime.Count > 0)
{
primeXOR ^= maxHeapPrime[0];
maxHeapPrime.RemoveAt(0);
}
if
(maxHeapNonPrime.Count > 0)
{
nonPrimeXor ^= maxHeapNonPrime[0];
maxHeapNonPrime.RemoveAt(0);
}
}
Console.WriteLine(
"Prime XOR = "
+ primeXOR);
Console.WriteLine(
"Composite XOR = "
+ nonPrimeXor);
}
static
void
Main() {
int
[] arr = { 4, 2, 12, 13, 5, 19 };
int
n = arr.Length;
int
k = 3;
kMinXOR(arr, n, k);
}
}