-
Notifications
You must be signed in to change notification settings - Fork 7
/
finding-frequency.py
96 lines (79 loc) · 2.26 KB
/
finding-frequency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
## Using Binary Search recursive
from itertools import groupby
def findingFreq(ar,n,q_ar,m):
ar=sorted(ar)
ar_distinct=set(ar)
ar_distinct = sorted(ar_distinct)
ar_distinct_lst = list(ar_distinct)
ar_distinct_cnt=[len(list(group)) for key, group in groupby(ar)]
def BS(i, q, q_ar, m, ar_distinct_lst, lo, hi):
mid = int((lo + hi) // 2)
if (lo>=hi and q != ar_distinct_lst[mid]):
return 'aditya'
elif (q == ar_distinct_lst[mid]):
return mid
elif (ar_distinct_lst[mid] < q_ar[i]):
return BS(i, q, q_ar, m, ar_distinct_lst, mid + 1, hi)
else:
return BS(i, q, q_ar, m, ar_distinct_lst, lo, mid)
for i in range(m):
lo=0; hi=len(ar_distinct_lst)-1
q=q_ar[i]
Qmid=BS(i,q,q_ar,m,ar_distinct_lst,lo,hi)
if Qmid=='aditya':
print(0)
else:
print(ar_distinct_cnt[Qmid])
n = int(input())
ar=list(map(int, input().split()))[:n]
m=int(input())
q_ar=[]
for i in range(m):
q_ar.append(int(input()))
findingFreq(ar,n,q_ar,m)
#######################################################################################################
## Using BinarySearch1 and BinarySearch2
def findingFreq(ar,n,q_ar,m):
ar_sorted=sorted(ar)
def BS1(ar_sorted,n,x):
p1=-1
lo=0
hi=n-1
while(lo<=hi):
mid=((lo+hi)//2)
if(ar_sorted[mid]<x):
lo=mid+1
elif (ar_sorted[mid]>x):
hi=mid-1
else:
p1=mid
hi=mid-1
return p1
def BS2(ar_sorted,n,x):
p2=-1
lo=0
hi=n-1
while(lo<=hi):
mid=((lo+hi)//2)
if(ar_sorted[mid]<x):
lo=mid+1
elif (ar_sorted[mid]>x):
hi=mid-1
else:
p2=mid
lo=mid+1
return p2
for i in range(m):
p1 = BS1(ar_sorted, n, q_ar[i])
p2 = BS2(ar_sorted, n, q_ar[i])
if (p1==-1 or p2==-1):
print(0)
else:
print(p2 - p1 + 1)
n = int(input())
ar=list(map(int, input().split()))[:n]
m=int(input())
q_ar=[]
for i in range(m):
q_ar.append(int(input()))
findingFreq(ar,n,q_ar,m)