XOR of K largest prime and composite numbers from the given array

Given an array arr[] of N non-zero positive integers and an integer K, the task is to find the XOR of the K largest prime and composite numbers.

Examples:

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

Approach: Using Sieve of Eratosthenes generate a boolean vector upto the size of the maximum element from the array which can be used to check whether a number is prime or not.
Now traverse the array and insert all the numbers which are prime in a max heap maxHeapPrime and all the composite numbers in max heap maxHeapNonPrime.
Now, pop out the top K elements from both the max heaps and take the xor of these elements.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function for Sieve of Eratosthenes
vector<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.
    vector<bool> 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<bool> prime = SieveOfEratosthenes(max_val);
  
    // Min Heaps to store the max K prime
    // and composite numbers
    priority_queue<int, vector<int>, greater<int> >
        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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<Integer> minHeapPrime = new PriorityQueue<>();
        PriorityQueue<Integer> 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

chevron_right


Output:

Prime XOR = 27
Composite XOR = 8


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : gp6, sanjeev2552