Picking Numbers

Sort by

recency

|

208 Discussions

|

  • + 0 comments

    Python solution

    def pickingNumbers(a):
        num_counter = Counter(a)
        return max(
            num_counter[num] + num_counter[num + 1]
            for num in num_counter.keys()
        )
    
  • + 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;
    
  • + 0 comments

    Problem: [1 ,2, 2, 3, 1, 2] Incorrect: Subsets [1,2,2,3] and [1,2] My output: 4 Expected output: 5

    Correct: because 3 doesn't fullfil condition to be in the same set as number 1 sorted: [1,1,2, 2, 2,3] Subsets [1,1,2,2,2] and [3] My output: 5 Expected output: 5

  • + 0 comments

    I interpreted this several other ways than what the problem statement meant before checking here lol...

  • + 1 comment

    question asks "longest subarray" that should be continuous elements from the original array, so not allowed to change order. Thats what the example shows. Then on input: 1 2 2 3 1 2 expected answer: 5.

    Thats for sure wrong. Question worded incorrectly. After sorting the inputarray my sollution passes. So the question means SUBSET: any combination of elements regardless of order or position