Recursive and non recursive simple O(n) dp
\n    // Not recursive 170ms\n    int maxSelectedElements(vector<int>& nums) {\n        const int n = size(nums);\n        ranges::sort(nums);\n        vector<v
```\n    // Not recursive 170ms\n    int maxSelectedElements(vector<int>& nums) {\n        const int n = size(nums);\n        ranges::sort(nums);\n        vector<vector<int>> dp(n, vector<int>(2, 1));\n        for (int i = 1; i < n; ++i) {\n            if (nums[i] == nums[i - 1]) {\n                dp[i][0] = dp[i - 1][0];\n                dp[i][1] = dp[i - 1][1];\n                dp[i][1] = max(dp[i][1], dp[i - 1][0] + 1);\n            } else if (nums[i] == nums[i - 1] + 2) {\n                dp[i][0] = dp[i - 1][1] + 1;\n            } else if (nums[i] == nums[i - 1] + 1) {\n                dp[i][1] = dp[i - 1][1] + 1;\n                dp[i][0] = dp[i - 1][0] + 1;\n            }\n        }\n        int ans = 0;\n        for (const auto& v: dp) {\n            ans = max(ans, max(v[0], v[1]));\n        }\n        return ans;\n    }\n\n    // Recursive 1000ms \n    int maxSelectedElements(vector<int>& nums) {\n        const int n = size(nums);\n        ranges::sort(nums);\n        vector<vector<int>> dp(n, vector<int>(2, -1));\n        function<int(int, bool isIncreased)> getOrCompute;\n        getOrCompute = [&](int i, bool isIncreased) {\n            if (i == n - 1) {\n                return 1;\n            }\n            if (dp[i][isIncreased] != -1) {\n                return dp[i][isIncreased];\n            }\n            if (nums[i + 1] > nums[i] + 1 + isIncreased) {\n                return dp[i][isIncreased] = 1;\n            }\n            int res = 0;\n            if (nums[i] + isIncreased > nums[i + 1]) {\n                return dp[i][isIncreased] = getOrCompute(i + 1, true);\n            }\n            if (nums[i + 1] == nums[i] + isIncreased) {\n                res = getOrCompute(i + 1, true);\n            } else if (nums[i + 1] == nums[i] + isIncreased + 1) {\n                res = getOrCompute(i + 1, false);\n            }\n            \n            return dp[i][isIncreased] = 1 + res;\n        };\n        int ans = 0;\n        for (int i = 0; i < n; ++i) {\n            ans = max(ans, max(getOrCompute(i, false), getOrCompute(i, true)));\n        }\n        return ans;\n    }\n```
['C++']