class Solution { public: vector<vector<int>> groupThePeople(vector<int>& gr) { vector<vector<int>>ans,ret; ans.resize(gr.size()+1); ans[gr[0]].push_back(0); for(int i=1;i<gr.size();i++){ ans[gr[i]].push_back(i); } for(int i=0;i<ans.size();i++){ if(ans[i].empty())continue; if(ans[i].size()>i){ for(int j=0;j<ans[i].size();j+=i){ vector<int>v(ans[i].begin()+j,ans[i].begin()+i>=ans[i].end()?ans[i].end():ans[i].begin()+j+i); ret.push_back(v); } } else ret.push_back(ans[i]); } return ret; } };