You are viewing a single comment's thread. Return to all comments →
C++ O(n log n) solution
int pickingNumbers(vector<int> a) { std::map<int, int> m; for(const int n : a) ++m[n]; if(m.size() == 1) return m.cbegin()->second; int max = 0; for(auto current = ++m.cbegin(); current != m.cend(); ++current) { auto prev = current; --prev; int elements = current->second; if(current->first - prev->first <= 1) elements += prev->second; if(elements > max) max = elements; } return max; }
Seems like cookies are disabled on this browser, please enable them to open this website
Picking Numbers
You are viewing a single comment's thread. Return to all comments →
C++ O(n log n) solution