Open In App

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

Last Updated : 08 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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;
}


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<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


Python3




# Python implementation 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<int> minHeapPrime = new List<int>();
    List<int> minHeapNonPrime = new List<int>();
 
    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




<script>
 
    // Javascript implementation of the approach
     
    // Function for Sieve of Eratosthenes
    function SieveOfEratosThenes(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.
      let prime = new Array(max_val + 1);
      for(let 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 (let 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 (let 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
    function kMinXOR(arr, n, k)
    {
 
      // Find maximum value in the array
      let max_val = Number.MIN_VALUE;
      for(let 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
      let prime = SieveOfEratosThenes(max_val);
 
      // Min Heaps to store the max K prime
      // and composite numbers
      let minHeapPrime = [];
      let minHeapNonPrime = [];
 
      for (let i = 0; i < n; i++)
      {
 
        // If current element is prime
        if (prime[arr[i]])
        {
 
          // Min heap will only store k elements
          if (minHeapPrime.length < 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[0] < arr[i])
          {
            minHeapPrime.shift();
            minHeapPrime.push(arr[i]);
          }
          minHeapPrime.sort(function(a, b){return a - b});
        }
 
        // If current element is composite
        else if (arr[i] != -1)
        {
 
          // Heap will only store k elements
          if (minHeapNonPrime.length < 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[0] < arr[i])
          {
            minHeapNonPrime.shift();
            minHeapNonPrime.push(arr[i]);
          }
          minHeapNonPrime.sort(function(a, b){return a - b});
        }
      }
 
      let primeXOR = 0, nonPrimeXor = 0;
 
      while (k-- > 0)
      {
 
        // Calculate the xor
        if (minHeapPrime.length > 0)
        {
          primeXOR ^= minHeapPrime[0];
          minHeapPrime.shift();
        }
 
        if (minHeapNonPrime.length > 0)
        {
          nonPrimeXor ^= minHeapNonPrime[0];
          minHeapNonPrime.shift();
        }
      }
 
      document.write("Prime XOR = " + primeXOR + "</br>");
      document.write("Composite XOR = " + nonPrimeXor);
    }
       
    let arr = [ 4, 2, 12, 13, 5, 19 ];
    let n = arr.length;
    let k = 3;
  
    kMinXOR(arr, n, k);
     
</script>


Output: 

Prime XOR = 27
Composite XOR = 8

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads