Easy O(n) Python3 Solution
Intuition\nFor each element in the array, we have two options:\n\n1. Increment it, and see what is the max chain this number contributes to\n2. Keep it the same
# Intuition\nFor each element in the array, we have two options:\n\n1. Increment it, and see what is the max chain this number contributes to\n2. Keep it the same, and see what is the max chain this contributes to\n\nWith the above in mind, it should point us in the direction that this could be solved with DP. \n\nFirst we sort the array, so we can start our chain count from the smallest possible element. We maintain a record of the longest number chain **ending** at the nums[i], and the longest number chain ending at nums[i] + 1. We do this by checking if nums[i] - 1 or nums[i] are in the array before.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution:\n    def maxSelectedElements(self, nums: List[int]) -> int:        \n        nums.sort()\n        ref = {}\n        max_value = 0\n        \n        for i in range(len(nums)):\n            num = nums[i]\n\n            # No point in processing any triplets of identical numbers\n            if i -2 >= 0 and nums[i-2] == num:\n                continue\n\n            if num - 1 in ref:\n                count = ref[num -1] + 1\n            else:\n                count = 1\n\n            prev = 1 if num not in ref else ref[num] + 1\n            ref[num] = count\n            ref[num + 1] = prev\n\n            max_value = max(max_value, ref[num], ref[num + 1])\n\n        return max_value\n            \n                \n```
['Dynamic Programming', 'Python', 'Python3']