Picking Numbers

Sort by

recency

|

192 Discussions

|

  • + 0 comments

    Python

    def pickingNumbers(a):
        # Write your code here
        record = []
        for element_original in sorted(a):
            l = []
            for element_copy in sorted(a):
                l.append(element_original - element_copy)
            record.append(max(l.count(0) + l.count(1), l.count(0) + l.count(-1)))
        return max(record)
    
  • + 0 comments

    The question should read: "give the length of the longest chain of numbers that meet the criterion that you can make with the numbers in the array".

  • + 0 comments

    C++ Solution: int pickingNumbers(vector a) {

    sort(a.begin(), a.end());
    
    int start = 0;
    int stop = 1;
    int res = 0;
    
    while(stop < a.size()) 
    {
        if(abs(a[start] - a[stop]) <= 1)
        {
            stop++;
        }
        else
        {
            res = max(res, stop - start);
            start = stop;
            stop++;
        }
    }
    
    res = max(res, stop - start);
    
    return res;
    

    }

  • + 1 comment

    Python

    def pickingNumbers(a):
        cnt = [0] * (max(a) + 1)
        for num in a:
            cnt[num] += 1
    
        res = 0
        for i in range(len(cnt) - 1):
            curr_sum = cnt[i] + cnt[i + 1]
            if curr_sum > res:
                res = curr_sum
        return res
    
  • + 0 comments

    Here is my python solution, the question is kind of confusing though.


    def pickingNumbers(a):
        # Write your code here
        a.sort()
        result = [[a[0]]]
        last_index = 1
        for item in result:
            for index in range(last_index, len(a)):
                last_index += 1
                if (abs(min(item) - a[index]) <= 1):
                    item.append(a[index])
                else:
                    result.append([a[index]])
                    break
        return len(sorted(result, key=lambda x: len(x))[-1])