HackerRank 'Picking Numbers' Solution

Martin Kysel · May 15, 2020

Short Problem Definition:

Given an array of integers, 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.

Picking Numbers


time complexity is O(N)

space complexity is O(N)


Calculate the occurrence of every element. The largest subset is the sum of two adjacent elements. For simplicity, I calculate the result in the same loop. It could be split out.

from collections import defaultdict

def pickingNumbers(a):
    d = defaultdict(int)
    r_val = 0
    for val in a:
        d[val] += 1
        r_val = max(r_val, d[val]+d[val+1], d[val]+d[val-1])

    return r_val

Twitter, Facebook