Created
March 9, 2020 00:01
-
-
Save BharathKumarS/6988f4dc9655cfff31cf8ef32d8fba3e to your computer and use it in GitHub Desktop.
HackerRank, Find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pickingNumbers(a): | |
b = list() | |
tempSum = pairSum = 0 | |
a.sort(reverse=True) | |
c = sorted(set(a),reverse=True) | |
for sortNum in c: | |
b.append([a.count(sortNum), sortNum]) | |
b.sort(key= lambda x:x[1], reverse=True) | |
for left in b: | |
for right in b: | |
if abs(left[1] - right[1]) <= 1 and left != right: | |
tempSum = left[0] + right[0] | |
if tempSum > pairSum: | |
pairSum = tempSum | |
if pairSum > max(b)[0]: | |
return(pairSum) | |
else: | |
return(max(b)[0]) | |
if __name__ == '__main__': | |
n = int(input().strip()) | |
a = list(map(int, input().rstrip().split())) | |
result = pickingNumbers(a) | |
print(str(result) + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment