Skip to content

Commit b3be2a0

Browse files
committed
Frequency Problems
1 parent d6547ce commit b3be2a0

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

Kth_frequency_problem.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Geek hosted a coding competition, some of the questions were easy and some of them were hard.
3+
You are given an array arr of positive integers of size N and an integer K, arr[i] represents the
4+
hardness of each problem of geeks' contest. Among those N numbers, your task is to find the numbers
5+
which appear more than K times and print them in increasing order. If no number appears more than K
6+
times than print -1.
7+
8+
Input:
9+
1. The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
10+
2. The first line of each test case contains two space-separated integers N and K.
11+
3. The second line contains N space-separated positive integers represents array arr.
12+
13+
Example:
14+
Input:
15+
3
16+
3 1
17+
5 5 6
18+
6 1
19+
2 2 3 3 4 4
20+
4 2
21+
1 2 2 3
22+
23+
Output:
24+
5
25+
2 3 4
26+
-1
27+
28+
"""
29+
30+
T = int(input())
31+
answer = []
32+
while(T > 0):
33+
ans=[]
34+
a = [int(x) for x in input().split()]
35+
arr = [int(x) for x in input().split()]
36+
l = a[0]
37+
k = a[1]
38+
f_arr = arr[:l]
39+
prop = set(f_arr)
40+
for x in prop:
41+
if f_arr.count(x) > k:
42+
ans.append(x)
43+
if len(ans) == 0:
44+
answer.append([0,-1])
45+
else:
46+
ans.sort()
47+
answer.append(ans)
48+
T = T - 1
49+
50+
for s in answer:
51+
for y in s:
52+
if y == 0:
53+
continue
54+
print(y,end=" ")
55+
print("")

frequency_of_elements.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
a = [1,2,3,1,3,5,6,1,2,5,3,7,8,4,2,7,9,4,5,2,1,5,2,3,7,8,4]
22
x = set(a)
33
for y in x:
4-
print("{} - {} times".format(y,a.count(y)))
4+
print("{} - {} times".format(y,a.count(y)))
5+
6+
"""
7+
input : a = [1,1,3,4,4]
8+
ouput : 2 0 1 2
9+
10+
Explain : 2 for 1 , 0 for 2 , 1 for 3 , 2 for 4
11+
"""
12+
13+
14+
def frequencycount(A,N):
15+
a = []
16+
for x in range(1,N + 1):
17+
a.append(A.count(x))
18+
for y in a:
19+
print(y,end=" ")
20+
21+
arr =[2,3,2,3,5]
22+
frequencycount(arr,5)

0 commit comments

Comments
 (0)