C++ Solution without using DP
Intuition\n- Calculated the Frequency of each value\n- If it\'s frequency is greater than 1, it can be used to create the next element\n- Starting from 1, check
# Intuition\n- Calculated the Frequency of each value\n- If it\'s frequency is greater than 1, it can be used to create the next element\n- Starting from 1, check how many elements can be consecutively created by keeping track of the value it\'s created from. Basically, 5 can be created using 5 or (4 + 1) if 4 is present.\n- Everytime we\'re not able to create a element value, the consecutive series breaks\n- In that condition, keep only those elements of series which were not created by previous value and remove everything else.\n\n\n# Code\n```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        unordered_map<int, int> freq;\n        int maxNum = 0;\n        for (int i : nums) {\n            ++freq[i];\n            maxNum = max(i+1, maxNum);\n        }\n\n        int count = 1;\n        vector<pair<int, int>> q;\n        for (int val = 1; val <= maxNum;) {\n            int curVal = val;\n\n            while (freq[val] || (!q.empty() && q.back().second == val)) {\n                if (!freq[val]) {\n                    ++val;\n                    continue;\n                }\n\n                if (!q.empty() && q.back().second == val)\n                    q.push_back({val, val + 1});\n                else {\n                    q.push_back({val, val});\n                    if (freq[val] > 1)\n                        q.push_back({val, val + 1});\n                }\n                val++;\n            }\n\n            int n = q.size();\n            count = max(count, n);\n\n            if (curVal == val)\n                ++val;\n            else {\n                int rem = n - 1;\n                for (; rem >= 0; --rem) {\n                    auto x = q[rem];\n                    if (x.first != x.second)\n                        break;\n                }\n                if(rem >= 0) q.erase(q.begin(), next(q.begin(), rem+1));\n                for (int i = 0; i < q.size(); ++i)\n                    ++q[i].second;\n            }\n        }\n\n        return count;\n    }\n};\n```
['C++']