Open In App

Numbers with prime frequencies greater than or equal to k

Last Updated : 02 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array, find elements that appear a prime number of times in the array with a minimum k frequency (frequency >= k).

Examples :  

Input : int[] arr = { 11, 11, 11, 23, 11, 37, 51, 
                      37, 37, 51, 51, 51, 51 };
        k = 2
Output : 37, 51
Explanation :
11's count is 4, 23 count 1, 37 count 3, 51 count 5. 
37 and 51 are two number that appear prime number of
time and frequencies greater than or equal to k.

Input : int[] arr = { 11, 22, 33 } min Occurrence = 1
Output : -1
None of the count is prime number of times 

Approach : 

  1. Create a Map that holds the number as Key and value as its occurrences in the input array. 
  2. Iterate the Map keys and look for the values corresponding to their keys, return the key which has minimum value fulfilling condition key’s value is a prime number and >= min occurrence provided 
    as input. 

Implementation:

C++




// C++ code to find number
// occurring prime number
// of times with frequency >= k
#include <bits/stdc++.h>
using namespace std;
 
// Check if the number of
// occurrences are primes
// or not
bool isPrime(int n)
{
    // Corner case
    if (n <= 1) return false;
 
    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
 
    return true;
}
 
// Function to find number
// with prime occurrences
void primeOccurrences(int arr[], int k)
{
    unordered_map<int, int> map;
     
    // Insert values and
    // their frequencies
    for (int i = 0; i < 12; i++)
        map[arr[i]]++;
 
    // Traverse map and find
    // elements with prime
    // frequencies and frequency
    // at least k
    for (auto x : map)
    {
        if (isPrime(x.second) &&
                    x.second >= k)
            cout << x.first << endl;
    }
}
 
// Driver code
int main()
{
    int arr[] = {11, 11, 11, 23,
                 11, 37, 37, 51,
                 51, 51, 51, 51};
    int k = 2;
 
    primeOccurrences(arr, k);
    return 0;
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


Java




// Java code to find number occurring prime
// number of times with frequency >= k
import java.util.*;
 
public class PrimeNumber {
 
    // Function to find number with prime occurrences
    static void primeOccurrences(int[] arr, int k)
    {
        Map<Integer, Integer> map = new HashMap<>();
 
        // Insert values and their frequencies
        for (int i = 0; i < arr.length; i++) {
            int val = arr[i];
 
            int freq;
            if (map.containsKey(val)) {
                freq = map.get(val);
                freq++;
            }
            else
                freq = 1;
            map.put(val, freq);
        }
 
        // Traverse map and find elements with
        // prime frequencies and frequency at
        // least k
        for (Map.Entry<Integer, Integer> entry :
                               map.entrySet()) {
            int value = entry.getValue();
            if (isPrime(value) && value >= k)
                System.out.println(entry.getKey());
        }
    }
 
    // Check if the number of occurrences
    // are primes or not
    private static boolean isPrime(int n)
    {
 
        if ((n > 2 && n % 2 == 0) || n == 1)
            return false;       
 
        for (int i = 3; i <= (int)Math.sqrt(n);
             i += 2) {
            if (n % i == 0)
                return false;           
        }
        return true;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 11, 11, 11, 23, 11, 37,
                      37, 51, 51, 51, 51, 51 };
        int k = 2;
 
        primeOccurrences(arr, k);
    }
}


Python3




# Python3 code to find number
# occurring prime number of
# times with frequency >= k
 
# Function to find number
# with prime occurrences
def primeOccurrences(arr, k):
    map = {}
 
    # Insert values and their frequencies
    for val in arr:
        freq = 0
         
        if val in map :
            freq = map[val]
            freq += 1
             
        else :
            freq = 1
        map[val] = freq
 
    # Traverse map and find elements
    # with prime frequencies and
    # frequency at least k
    for entry in map :
        value = map[entry]
         
        if isPrime(value) and value >= k:
            print(entry)
 
# Check if the number of occurrences
# are primes or not
def isPrime(n):
 
    if (n > 2 and not n % 2) or n == 1:
        return False    
 
    for i in range(3, int(n**0.5 + 1), 2):
        if not n % i:
            return False
             
    return True
 
 
# Driver code
 
arr = [ 11, 11, 11, 23, 11, 37,
        37, 51, 51, 51, 51, 51 ]
k = 2
 
primeOccurrences(arr, k)
 
 
# This code is contributed by Ansu Kumari.


C#




// C# code to find number
// occurring prime number
// of times with frequency >= k
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to find number
    // with prime occurrences
    static void primeOccurrences(int[] arr,
                                int k)
    {
        Dictionary<int, int> map =
                   new Dictionary<int, int>();
         
        // Insert values and
        // their frequencies
        for (int i = 0; i < arr.Length; i++)
        {
            int val = arr[i];
 
            int freq;
            if (map.ContainsKey(val))
            {
                freq = map[val];
                freq++;
                map.Remove(val);
            }
            else
                freq = 1;
            map.Add(val, freq);
        }
 
        // Traverse map and find elements
        // with prime frequencies and
        // frequency atleast k
        foreach (KeyValuePair<int, int>
                           pair in map)
        {
            int value = pair.Value;
            if (isPrime(value) &&
                        value >= k)
                Console.WriteLine(pair.Key);
        }
    }
 
    // Check if the number
    // of occurrences
    // are primes or not
    static bool isPrime(int n)
    {
        if ((n > 2 &&
             n % 2 == 0) || n == 1)
            return false;    
 
        for (int i = 3;
                 i <= (int)Math.Sqrt(n);
                 i += 2)
            {
                if (n % i == 0)
                    return false;        
            }
        return true;
    }
 
    // Driver code
    static void Main()
    {
        int[] arr = new int[]{11, 11, 11, 23, 11, 37,
                              37, 51, 51, 51, 51, 51};
        int k = 2;
 
        primeOccurrences(arr, k);
    }
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


Javascript




<script>
// Javascript code to find number
// occurring prime number
// of times with frequency >= k
 
// Check if the number of
// occurrences are primes
// or not
function isPrime(n) {
    // Corner case
    if (n <= 1) return false;
 
    // Check from 2 to n-1
    for (let i = 2; i < n; i++)
        if (n % i == 0)
            return false;
 
    return true;
}
 
// Function to find number
// with prime occurrences
function primeOccurrences(arr, k) {
    let map = new Map();
 
    // Insert values and
    // their frequencies
    for (let i = 0; i < arr.length; i++) {
        let val = arr[i];
 
        let freq;
        if (map.has(val)) {
            freq = map.get(val);
            freq++;
        }
        else
            freq = 1;
        map.set(val, freq);
    }
 
    // Traverse map and find
    // elements with prime
    // frequencies and frequency
    // at least k
    for (let x of map) {
        if (isPrime(x[1]) &&
            x[1] >= k)
            document.write(x[0] + "<br>");
    }
}
 
// Driver code
 
let arr = [11, 11, 11, 23,
           11, 37, 37, 51,
              51, 51, 51, 51];
let k = 2;
 
primeOccurrences(arr, k);
 
// This code is contributed by
// gfgking
 
</script>


Output : 

37
51

 

Time Complexity: O(n), where n is number of elements in the array.
Auxiliary Space: O(n), where n is number of elements in the array.



Similar Reads

Length of longest subarray in which elements greater than K are more than elements not greater than K
Given an array arr[] of length N. The task is to find the length of the longest subarray in which elements greater than a given number K are more than elements not greater than K.Examples: Input : N = 5, K = 2, arr[]={ 1, 2, 3, 4, 1 } Output : 3 The subarray [2, 3, 4] or [3, 4, 1] satisfy the given condition, and there is no subarray of length 4 or
10 min read
Sum of elements in an array with frequencies greater than or equal to that element
Given an array arr[] of N integers. The task is to find the sum of the elements which have frequencies greater than or equal to that element in the array. Examples: Input: arr[] = {2, 1, 1, 2, 1, 6} Output: 3 The elements in the array are {2, 1, 6} Where, 2 appear 2 times which is greater than equal to 2 itself. 1 appear 3 times which is greater th
9 min read
Count numbers in a given range having prime and non-prime digits at prime and non-prime positions respectively
Given two integers L and R, the task is to find the count of numbers in the range [L, R] having prime digits at prime positions and non-prime digits at non-prime positions. Examples: Input: L = 5, R = 22 Output: 7Explanation: The numbers 6, 8, 9, 12, 13, 15, and 17 have prime digits at prime positions and non-prime digits at non-prime positions. In
15 min read
Consecutive Prime numbers greater than equal to given number.
Question: Given a number n, the task is to find two consecutive prime such that the product of these two prime is greater than or equal to n. Example: Input: 14Output: 3 5Explanation: 3 and 5 are consecutive prime numbers whose product is greater than 14. Approach: Suppose n is of the range 10^8 to 10^10. We cannot find out primes using sieve becau
7 min read
Smallest subarray of size greater than K with sum greater than a given value
Given an array, arr[] of size N, two positive integers K and S, the task is to find the length of the smallest subarray of size greater than K, whose sum is greater than S. Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 1, S = 8Output: 2Explanation: Smallest subarray with sum greater than S(=8) is {4, 5}Therefore, the required output is 2. Input: ar
15 min read
Smallest Special Prime which is greater than or equal to a given number
Given a number N. The task is to find the smallest special prime which is greater than or equal to N.A special prime is a number which can be created by placing digits one after another such the all the resulting numbers are prime. Examples: Input: N = 379 Output: 379 379 can be created as =&gt; 3 =&gt; 37 =&gt; 379 Here, all the numbers ie. 3, 37,
9 min read
Highest and Smallest power of K less than and greater than equal to N respectively
Given positive integers N and K, the task is to find the highest and smallest power of K greater than equal to and less than equal to N respectively. Examples: Input: N = 3, K = 2 Output: 2 4 Highest power of 2 less than 3 = 2 Smallest power of 2 greater than 3 = 4 Input: N = 6, K = 3 Output: 3 9 Highest power of 3 less than 6 = 3 Smallest power of
5 min read
Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
Given an integer N, the task is to print N integers ? 109 such that no two consecutive of these integers are co-prime and every 3 consecutive are co-prime. Examples: Input: N = 3 Output: 6 15 10Input: N = 4 Output: 6 15 35 14 Approach: We can just multiply consecutive primes and for the last number just multiply the gcd(last, last-1) * 2. We do thi
22 min read
Different ways to sum n using numbers greater than or equal to m
Given two natural number n and m. The task is to find the number of ways in which the numbers that are greater than or equal to m can be added to get the sum n. Examples: Input : n = 3, m = 1 Output : 3 Following are three different ways to get sum n such that each term is greater than or equal to m 1 + 1 + 1, 1 + 2, 3 Input : n = 2, m = 1 Output :
7 min read
First element greater than or equal to X in prefix sum of N numbers using Binary Lifting
Given an array of N integers and a number X. The task is to find the index of first element which is greater than or equal to X in prefix sums of N numbers. Examples: Input: arr[] = { 2, 5, 7, 1, 6, 9, 12, 4, 6 } and x = 8 Output: 2 prefix sum array formed is { 2, 7, 14, 15, 21, 30, 42, 46, 52}, hence 14 is the number whose index is 2 Input: arr[]
9 min read
Article Tags :
Practice Tags :