-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpickingNumbers.py
61 lines (41 loc) · 1.06 KB
/
pickingNumbers.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
#!/bin/python3
# ALDO FUSTER TURPIN
import math
import os
import random
import re
import sys
def createFrequencyMap(arr):
freq = {}
for num in arr:
if num in freq:
freq[num] += 1
else:
freq[num] = 1
return freq
def pickingNumbers(a):
freq = createFrequencyMap(a)
items = sorted(freq.items())
if len(items) == 1:
return items[0][1]
i = 1
maxLength = 0
while(i < len(items)):
count1 = items[i][1]
maxLength = max(maxLength, count1)
count2 = items[i-1][1]
maxLength = max(maxLength, count2)
num1 = items[i][0]
num2 = items[i-1][0]
if abs(num1 - num2) <= 1:
sum = count1 + count2
maxLength = max(maxLength, sum)
i += 1
return maxLength
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
a = list(map(int, input().rstrip().split()))
result = pickingNumbers(a)
fptr.write(str(result) + '\n')
fptr.close()