• + 0 comments

    2 versions of Python code solving in O(n) time:

    from collections import Counter
    
    def pickingNumbers(a):
        c = Counter(a)
        return max( c[n]+c.get(n-1,0) for n in c )
    

    version 2:

    def pickingNumbers1(a):
        a = sorted(a)
        m=i=0
        while i<len(a):
            s=i
            while i<len(a) and abs(a[i]-a[s])<=1: i+=1
            m = i-s if i-s>m else m
        return m