Numbers with prime frequencies greater than or equal to k
Last Updated :
02 Aug, 2022
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 :
- Create a Map that holds the number as Key and value as its occurrences in the input array.
- 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++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1) return false ;
for ( int i = 2; i < n; i++)
if (n % i == 0)
return false ;
return true ;
}
void primeOccurrences( int arr[], int k)
{
unordered_map< int , int > map;
for ( int i = 0; i < 12; i++)
map[arr[i]]++;
for ( auto x : map)
{
if (isPrime(x.second) &&
x.second >= k)
cout << x.first << endl;
}
}
int main()
{
int arr[] = {11, 11, 11, 23,
11, 37, 37, 51,
51, 51, 51, 51};
int k = 2;
primeOccurrences(arr, k);
return 0;
}
|
Java
import java.util.*;
public class PrimeNumber {
static void primeOccurrences( int [] arr, int k)
{
Map<Integer, Integer> map = new HashMap<>();
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);
}
for (Map.Entry<Integer, Integer> entry :
map.entrySet()) {
int value = entry.getValue();
if (isPrime(value) && value >= k)
System.out.println(entry.getKey());
}
}
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 ;
}
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
def primeOccurrences(arr, k):
map = {}
for val in arr:
freq = 0
if val in map :
freq = map [val]
freq + = 1
else :
freq = 1
map [val] = freq
for entry in map :
value = map [entry]
if isPrime(value) and value > = k:
print (entry)
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
arr = [ 11 , 11 , 11 , 23 , 11 , 37 ,
37 , 51 , 51 , 51 , 51 , 51 ]
k = 2
primeOccurrences(arr, k)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void primeOccurrences( int [] arr,
int k)
{
Dictionary< int , int > map =
new Dictionary< int , int >();
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);
}
foreach (KeyValuePair< int , int >
pair in map)
{
int value = pair.Value;
if (isPrime(value) &&
value >= k)
Console.WriteLine(pair.Key);
}
}
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 ;
}
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);
}
}
|
Javascript
<script>
function isPrime(n) {
if (n <= 1) return false ;
for (let i = 2; i < n; i++)
if (n % i == 0)
return false ;
return true ;
}
function primeOccurrences(arr, k) {
let map = new Map();
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);
}
for (let x of map) {
if (isPrime(x[1]) &&
x[1] >= k)
document.write(x[0] + "<br>" );
}
}
let arr = [11, 11, 11, 23,
11, 37, 37, 51,
51, 51, 51, 51];
let k = 2;
primeOccurrences(arr, k);
</script>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...