Consecutive group formation in Array of dimension N

Consecutive group formation in Array of dimension N

#embody <bits/stdc++.h>

#embody <iostream>

utilizing namespace std;

  

bool checkGroups(vector<int>& arr, int Okay)

{

  

    

    

    if (arr.dimension() % Okay != 0) {

        return false;

    }

  

    

    map<int, int> m;

  

    int i = 0;

    whereas (i < arr.dimension()) {

        m[arr[i]]++;

        i++;

    }

  

    int len = arr.dimension();

  

    whereas (len > 0) {

        int kind = 0;

        int prev = -1;

        for (auto a : m) {

  

            

            

            if (prev == -1) {

                prev = a.first;

            }

            else if (prev + 1 == a.first) {

                prev = a.first;

            }

            else {

                return false;

            }

            kind++;

            m[a.first]--;

            if (m[a.first] == 0) {

                m.erase(a.first);

            }

  

            

            

            if (kind == Okay) {

                break;

            }

        }

        if (kind != Okay) {

            return false;

        }

        len -= Okay;

    }

    if (len == 0) {

        return true;

    }

    return false;

}

  

int most important()

{

  

    vector<int> arr = { 1, 2, 3, 6, 2, 3, 4, 7, 8 };

  

    int Okay = 3;

  

    

    cout << checkGroups(arr, Okay);

  

    return 0;

}