-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorting-Fraudulent Activity Notifications.py
71 lines (51 loc) · 1.71 KB
/
Sorting-Fraudulent Activity Notifications.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
#Problem Link : https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=sorting
#Ans:
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'activityNotifications' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY expenditure
# 2. INTEGER d
#
def activityNotifications(expenditure, d):
count = 0
count_array = [0] * 201
for i in range(d):
count_array[expenditure[i]] += 1
for i in range(d, len(expenditure)):
if expenditure[i] >= getMedianVal(count_array, d) * 2:
count += 1
count_array[expenditure[i - d]] -= 1
count_array[expenditure[i]] += 1
return count
def getMedianVal(count_arr, d):
is_length_even = d % 2 == 0
total_count = 0
for i, count in enumerate(count_arr):
total_count += count
if is_length_even:
if total_count == d // 2:
for j in range(i + 1, len(count_arr)):
if count_arr[j] > 0:
return (i + j) / 2
elif total_count > d // 2:
return i
else:
if total_count >= d // 2 + 1:
return i
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
d = int(first_multiple_input[1])
expenditure = list(map(int, input().rstrip().split()))
result = activityNotifications(expenditure, d)
fptr.write(str(result) + '\n')
fptr.close()