DP Solution
Approach\nStore two arrays:\nL - keeps track of the largest consecutive subsequence that can be formed using elements 0,..,i without incremented nums[i]\nI - si
# Approach\nStore two arrays:\nL - keeps track of the largest consecutive subsequence that can be formed using elements 0,..,i without incremented nums[i]\nI - similar to L, but nums[i] is incremented\n\nSort nums.\nThen, we iterate through each index 0,..,n\nFor each index i, we can either increase nums[i] or not\n\nCase 1 : we increment nums[i]\n- then the previous element in the sequence ending at index i must have value nums[i] (so we\'d form the sequence nums[i], nums[i]+1)\n- this happens if we find a j so nums[j] = nums[i] exactly\n- OR nums[j] = nums[i] - 1, and we consider when j was incremented\n\nCase 2 : without incrementing nums[i]\n- then the previous element in the sequence ending at i must have value nums[i] - 1 (so we\'d form the sequence nums[i]-1, nums[i])\n- this happends if we find a j so that nums[j] = nums[i] - 1 exactly\n- OR nums[j] = nums[i] - 2, and we consider when j was incremented\n\nThen, we find the maximum length of any subsequence that can be found ending with any index and whether that element was incremented or note (so maximize over the arrays L and I)\n\n# Code\n```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        int n = nums.size();\n        vector<int> L (n, 0); // max length end at i, if nums[i] not incr\n        vector<int> I (n, 0); // max length end at i, if nums[i] was incr\n\n        sort(nums.begin(), nums.end());\n        int ret = 1;\n\n        for (int i=0; i<n; ++i)\n        {\n            L[i] = 1; // at least 1, item itself\n            I[i] = 1;\n\n            // check previous elements that are same or, 1 away from curr i\n            int j = i-1;\n            while (j >= 0)\n            {\n                // if increased\n                if (nums[j] == nums[i])\n                    I[i] = max(I[i], L[j] + 1);\n                else if (nums[i] - nums[j] == 1)\n                {\n                    // i incr & j incr\n                    I[i] = max(I[i], I[j] + 1);\n                    // i not & j not\n                    L[i] = max(L[i], L[j] + 1);\n                } else if (nums[i] - nums[j] == 2)\n                {\n                    // i not incr, j incr\n                    L[i] = max(L[i], I[j] +1);\n                }\n                if (nums[i] - nums[j] > 2) break;\n                --j;\n            }\n            //cout << "i : " << i << " not: " << L[i] << " inc: " << I[i] << endl;\n            ret = max(ret, max(L[i], I[i]));\n        }\n        return ret;\n    }\n};\n```
['C++']