Python: Dynamic Programming beats 100%
\nclass Solution:\n    def maxSelectedElements(self, nums: List[int]) -> int:\n\n        nums = sorted(nums)\n        dp = [[1, 1] for i in range(len(nums))]\n\
```\nclass Solution:\n    def maxSelectedElements(self, nums: List[int]) -> int:\n\n        nums = sorted(nums)\n        dp = [[1, 1] for i in range(len(nums))]\n\n        """\n            dp[i][0] = maximum consecutive elements in an array till index i w/o modifying nums[i]\n            dp[i][1] = maximum consecutive elements in an array till index i after modifying nums[i]\n        """\n\n        for i in range(1, len(nums)):\n            curr, prev = nums[i], nums[i - 1]\n\n            if curr == prev:\n                dp[i] = dp[i - 1][::]\n                dp[i][1] = max(dp[i][1], dp[i-1][0] + 1)\n\n            if curr - prev == 1:\n                dp[i][0] = max(dp[i][0], dp[i-1][0] + 1)\n                dp[i][1] = max(dp[i][1], dp[i-1][1] + 1)\n\n            if curr - prev == 2:\n                dp[i][0] = max(dp[i][0], dp[i-1][1] + 1)\n        \n        return max(max(a, b) for a, b in dp)\n```
['Python3']