Skip to content

Instantly share code, notes, and snippets.

@BharathKumarS
Created March 9, 2020 00:01
Show Gist options
  • Save BharathKumarS/6988f4dc9655cfff31cf8ef32d8fba3e to your computer and use it in GitHub Desktop.
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
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