fork(4) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int maxSubGroups(vector<int>arr){
  5. int n=arr.size();
  6. vector<int>sortedArr = arr;
  7. sort(sortedArr.begin(), sortedArr.end());
  8. map<int,int>m;
  9. for(int i=0;i<sortedArr.size();i++)
  10. m[sortedArr[i]]=i;
  11. int maxGroups = 0, maxIndex=0;
  12. for(int i=0;i<n;i++){
  13. maxIndex = max(maxIndex, m[arr[i]]);
  14. if(maxIndex == i)
  15. maxGroups++;
  16. }
  17. return maxGroups;
  18. }
  19.  
  20. int main() {
  21. vector<int>arr = {1, 5, 4, 9, 8, 7, 12, 13, 14};
  22. int maxGroups = maxSubGroups(arr);
  23. cout<<maxGroups;
  24. return 0;
  25. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
6