• + 0 comments

    As many exercises on this site, the description of the problem is misleading: it gives you the impression that data in the array have to stay unordered... but in reality the problem expects for you to sort the array.

    def pickingNumbers(a):
        a.sort()
        cnt = 1
        cmx = 0
        i = 1
        s = a[0]
        while i<len(a):
            t = a[i]
            if abs(t-s)<=1:
                cnt += 1
            else:
                s = a[i]
                cmx = max(cmx,cnt)
                cnt = 1
            i += 1
        if i == len(a):
            cmx = max(cmx, cnt)
        return cmx