Picking Numbers

  • + 0 comments

    I'm seeing a lot of dictionaries in the comments, but because the constraints declare the numbers are within 1 - 100, we can use a fast counting array, here's my C# solution.

        const int lim = 100;
        int[] count = new int[lim];
        for(int i = 0; i < a.Count; ++i) ++count[a[i]];
        int max = 0;
        for(int i  =1; i < lim; ++ i)
        {
            max = Math.Max(count[i]+count[i-1],max);
        }
        return max;