Array elements with prime frequencies?

In this problem, we need to count how many distinct elements in an array appear a prime number of times. For example, if the array is {1, 2, 2, 0, 1, 5, 2, 5, 0, 0, 1, 1}, then 1 appears 4 times (not prime), 2 appears 3 times (prime), 0 appears 3 times (prime), and 5 appears 2 times (prime). So there are three elements {2, 0, 5} with prime frequencies, giving us a count of 3.

Syntax

int countPrimeOccurrence(int arr[], int n);
bool isPrime(int n);

Algorithm

countPrimeOccurrence(arr, n)

Begin
   count := 0
   create frequency array to count occurrences
   for each element e in arr, do
      increment frequency[e]
   done
   for each frequency value, check if it is prime
      if prime, then increment count
   return count
End

Example

Here's a complete C program to solve this problem −

#include <stdio.h>
#include <stdbool.h>

bool isPrime(int n) {
    if (n <= 1) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;
    
    for (int i = 3; i * i <= n; i += 2) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

int countPrimeOccurrence(int arr[], int n) {
    int count = 0;
    int freq[1000] = {0}; // Assuming array elements are within range [0, 999]
    int min_val = arr[0], max_val = arr[0];
    
    // Find min and max values
    for (int i = 0; i < n; i++) {
        if (arr[i] < min_val) min_val = arr[i];
        if (arr[i] > max_val) max_val = arr[i];
    }
    
    // Count frequencies (handle negative numbers by offsetting)
    int offset = (min_val < 0) ? -min_val : 0;
    for (int i = 0; i < n; i++) {
        freq[arr[i] + offset]++;
    }
    
    // Count elements with prime frequencies
    for (int i = 0; i <= max_val + offset; i++) {
        if (freq[i] > 0 && isPrime(freq[i])) {
            count++;
        }
    }
    
    return count;
}

int main() {
    int arr[] = {1, 2, 2, 0, 1, 5, 2, 5, 0, 0, 1, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    printf("Array elements: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("<br>");
    
    printf("Prime frequency count: %d<br>", countPrimeOccurrence(arr, n));
    return 0;
}
Array elements: 1 2 2 0 1 5 2 5 0 0 1 1 
Prime frequency count: 3

How It Works

  • isPrime() function: Checks if a number is prime by testing divisibility up to its square root
  • Frequency counting: Uses an array to count occurrences of each element
  • Offset handling: Handles negative numbers by using an offset to map them to positive indices
  • Prime checking: For each non-zero frequency, checks if it's prime and increments the counter

Key Points

  • Time complexity: O(n + k?m) where n is array size, k is unique elements, and m is maximum frequency
  • Space complexity: O(range) for the frequency array
  • The solution assumes array elements are within a reasonable range for memory efficiency

Conclusion

This algorithm efficiently counts array elements that appear a prime number of times by using frequency counting and prime checking. The approach works well for arrays with elements in a bounded range.

Updated on: 2026-03-15T11:16:39+05:30

514 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements