Intution + Approach DP & Sorting
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intution for this problem was to consider every number to stay in two states, this 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intution for this problem was to consider every number to stay in two states, this is `num[i], or nums[i] + 1`, now consider the defination of `DP` variable as **`DP[a]` = maximum length of consicutive nums we can create by considering `a` as the end of that sequence**\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nNow if you sort the `nums` and apply iteratively the upper defination of DP and keep on finding the maximum value for `DP[nums[i]]` or `DP[nums[i] + 1]` by this approach `DP[nums[i]] = max(DP[nums[i] - 1], DP[nums[i]])` you can find the ans, by taking max of `DP`.\n\n# Example\n`nums = [2,1,5,1,1]`\nsort nums\n`nums = [1,1,1,2,5]`\n`i = 0`: `DP[nums[i]] i.e. DP[1] = 1, and DP[2] = 1`, `ans = 1`\n`i = 1`: `DP[1] = 1, and DP[2] = 2` `ans = 2`\n`i = 2`: `DP[1] = 1, and DP[2] = 2` `ans = 2`\n`i = 3`: `DP[1] = 1, DP[2] = 2, and DP[3] = 3` `ans = 3`\n`i = 4`: `DP[1] = 1, DP[2] = 2, DP[3] = 3, DP[5] = 1, and DP[6] = 1` `ans = 3`\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(nlogn)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n# Code\n**C++**\n```\nclass Solution {\npublic:\n    Solution() {\n        ios::sync_with_stdio(false);\n    }\n\n    int maxSelectedElements(vector<int>& nums) {\n        int n = nums.size(), ans = 1;\n        sort(nums.begin(), nums.end());\n        map<int, int> DP;\n\n        for (int i = 0; i < n; i++) {\n            int a = 1, b = 1;\n\n            if (DP.find(nums[i] - 1) != DP.end()) {\n                a = DP[nums[i] - 1] + 1;\n            }\n            \n            if (DP.find(nums[i]) != DP.end()) {\n                b = DP[nums[i]] + 1;\n            }\n            \n            if (DP.find(nums[i]) == DP.end()) DP[nums[i]] = a; else DP[nums[i]] = max(DP[nums[i]], a);\n            if (DP.find(nums[i] + 1) == DP.end()) DP[nums[i] + 1] = b; else DP[nums[i] + 1] = max(DP[nums[i] + 1], b);\n            \n            ans = max(max(DP[nums[i]], DP[nums[i] + 1]), ans);\n        }\n        \n        return ans;\n    }\n};\n```\n**Python**\n```\nclass Solution:\n    def maxSelectedElements(self, nums: List[int]) -> int:\n        nums.sort()\n        n = len(nums)\n        ans = 1\n        DP = {}\n        \n        for num in nums:\n            a, b = 1, 1\n            if num - 1 in DP:\n                a = DP[num - 1] + 1\n            \n            if num in DP:\n                b = DP[num] + 1\n                \n            DP[num] = max(DP.get(num, 0), a)\n            DP[num + 1] = max(DP.get(num + 1, 0), b)\n            \n            ans = max(ans, max(DP[num], DP[num + 1]))\n        \n        return ans\n        \n```
['Dynamic Programming', 'Sorting', 'C++', 'Python3']