• + 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;
    }