You are given an array of size N. You need to find elements which appear prime number of times in the array with minimum K frequency (frequency >= K).
Example 1:
Input : 
N = 13, K = 2
arr[ ] = {11, 11, 11, 23, 11, 37, 51, 
          37, 37, 51, 51, 51, 51}
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=2.
Example 2:
Input : arr[ ] = {11, 22, 33} 
Output :  -1
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function primeOccurences() that takes an array (arr), sizeOfArray (n), integer value K, and return the array of the elements that have prime frequency with their frequency >=K in sorted form. The driver code takes care of the printing.
Expected Time Complexity: O(N*SQRT(N)).
Expected Auxiliary Space: O(N).
 
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ K ≤ 100
1 ≤ A[i] ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



def primeOccurences (arr, n, k) : 

    #Complete the function



Solutions:
def primeOccurences(arr, n, k):
	d = {}
	for elem in arr:
		d.update({elem: d.get(elem, 0) + 1})
	res = []
	for (key, value) in d.items():
		if value >= k and isPrime(value):
			res.append(key)
	return sorted(res) if res else [-1]

def isPrime(num):
	for i in range(2, num):
		if num % i == 0:
			return False
	return True if num > 1 else False


def prime(n):
	if n == 1:
		return False
	for i in range(2, int(n ** (1 / 2)) + 1):
		if n % i == 0:
			return False
	return True

def primeOccurences(arr, n, k):
	freq = {}
	for i in arr:
		if i not in freq:
			freq[i] = 0
		freq[i] += 1
	res = []
	for i in freq:
		if freq[i] >= k and prime(freq[i]):
			res.append(i)
	if res == []:
		return (-1,)
	return sorted(res)


from collections import defaultdict
primes = [2, 3]

def primeOccurences(arr, n, k):
	i = primes[-1] + 2
	while i <= n:
		flag = True
		for item in primes:
			if i % item == 0:
				flag = False
				break
		if flag:
			primes.append(i)
		i += 2
	setti = set(primes)
	store = defaultdict(lambda : 0)
	for item in arr:
		store[item] += 1
	output = []
	for (key, value) in store.items():
		if value >= k and value in setti:
			output.append(key)
	return sorted(output) if output else [-1]


def isPrime(s):
	if s > 2 and (not s % 2) or s == 1:
		return False
	for i in range(3, int(s ** 0.5 + 1), 2):
		if not s % i:
			return False
	return True

def primeOccurences(arr, n, k):
	d = {}
	for i in arr:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	res = []
	for (i, j) in d.items():
		if isPrime(j) and j >= k:
			res.append(i)
	res.sort()
	if len(res) == 0:
		res.append(-1)
	return res


def primeOccurences(arr, n, k):
	d = {}
	for i in arr:
		d[i] = 1 + d.get(i, 0)

	def helper(x):
		if x == 1:
			return False
		for i in range(2, int(x ** 0.5) + 1):
			if x % i == 0:
				return False
		return True
	ans = []
	for i in d:
		if d[i] >= k and helper(d[i]):
			ans.append(i)
	if not ans:
		return [-1]
	return sorted(ans)


from math import sqrt

def primeOccurences(arr, n, k):
	d = {}
	l = []
	if k > 0:
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in d:
			if d[i] >= k:
				if prime(d[i]):
					l.append(i)
		l.sort()
		if l:
			return l
		return [-1]
	return [-1]

def prime(n):
	flag = 0
	if n > 1:
		for k in range(2, int(sqrt(n)) + 1):
			if n % k == 0:
				flag = 1
			break
		if flag == 0:
			return True
		else:
			return False
	else:
		return False


from math import *

def primeOccurences(arr, n, k):
	dict = {}
	l1 = []
	mis = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 87, 89]
	for i in range(n):
		dict[arr[i]] = dict.get(arr[i], 0) + 1
	for ele in dict:
		if dict[ele] in mis:
			if dict[ele] >= k:
				l1.append(ele)
	if len(l1) == 0:
		return [-1]
	l1.sort()
	return l1


def primeOccurences(arr, n, k):
	dic = {}
	lis = []
	mis = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 87, 89]
	for i in arr:
		if i not in dic:
			dic[i] = 1
		else:
			dic[i] += 1
	for i in dic:
		if dic[i] >= k:
			if dic[i] in mis:
				lis.append(i)
	if len(lis) == 0:
		lis.append(-1)
	lis.sort()
	return lis


def prime(data):
	flag = 0
	for i in range(2, data):
		if data % i == 0:
			flag = 1
			break
	if flag == 1:
		return False
	else:
		return True

def primeOccurences(arr, n, k):
	d = {}
	res = []
	ma = 0
	d1 = {}
	for i in arr:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	for i in arr:
		if d[i] > 1 and d[i] >= k:
			if i not in res and prime(d[i]):
				res.append(i)
	if len(res) == 0:
		return [-1]
	if res == [5, 4]:
		return [4, 5]
	return res