Sort by

recency

|

2534 Discussions

|

  • + 1 comment

    For Sample Input 0

    6 4 6 5 3 3 1

    {4,3,3} is NOT a subarray of the input array. Subarrays are contiguous. Does anyone proofread these before they are posted?

    I'd vent more but this page appears to not have been tested on Safari. FFS. Ignore all text bdlow this that I can't access when editing.

    3 Explanation 0 We choose the following multiset of integers from the array: . Each pair in the multiset has an absolute difference (i.e., and ), so we print the number of chosen integers, , as our answer.

  • + 0 comments

    C#: O(nlogn)
    public static int pickingNumbers(List a) { if (a.Count <= 1) return a.Count;

    a.Sort(); 
    int result = 0;
    int currentCount = 1;
    
    for (int i = 0, j = 1; j < a.Count; j++)
    {
    
        if (a[j] - a[i] <= 1)
        {
            currentCount++;
        }
        else
        {
    
            result = Math.Max(result, currentCount);
            currentCount = 1; 
            i = j; 
        }
    }
    
    result = Math.Max(result, currentCount);
    return result;
    }
    

    }

  • + 1 comment

    my python solution.:

    from collections import Counter
    
    def pickingNumbers(a):
        x = Counter(a)
        maxlen = 0
        for i in x:
            current_len = x[i] + x.get(i+1, 0)
            maxlen = max(current_len, maxlen)
            
        return maxlen
    
  • + 0 comments

    Here is my O(n) c++ solution, you can find the explanation here : https://youtu.be/0zvqEO1gDRw

    int pickingNumbers(vector<int> a) {
        map<int, int> mp;
        for(int e : a) mp[e]++;
        int ans = mp[0];
        for(int i = 1; i < 99; i++){
            int curr = max(mp[i] + mp[i+1], mp[i] + mp[i-1]);
            ans = max(ans, curr);
        }
        return ans;
    }
    
  • + 0 comments

    solution Java.

    public static int pickingNumbers(List<Integer> a) {
        // Write your code here
            Collections.sort(a);
            int l = 0;
            int r = 1;
            int score = 0;
            
            while( l<r && r < a.size() ){
                if (a.get(r) - a.get(l) <= 1){
                    score = Math.max(score , r - l + 1);
                }else{
                    l = r;
                }
                r++;
            }
            
            return score;
            
        }