LC 1399. Count Largest Group

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
private:
    int digitSum(int n){
        int cnt = 0;
        while(n){
            cnt += (n%10);
            n/=10;
        }
        return cnt;
    }
public:
    int countLargestGroup(int n) {
        vector<vector<int>> store;
        vector<int> len_store;
        for(int i=0;i<=n;i++){
            vector<int> num;
            store.push_back(num);
        }
        for(int i=1;i<=n;i++){
            store[digitSum(i)].push_back(i);
        }
        for(int i=1;i<=n;i++){
            len_store.push_back(store[i].size());
        }
        sort(len_store.begin(),len_store.end());
        return count(len_store.begin(),len_store.end(),len_store[len_store.size()-1]);
        return 0;
    }
};


Dicussion Forum