DP Solution || O(N) || Iterative Approach
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxSelectedElements = function(nums) {\n  nums.sort((a,b) => a-b);\n  let dp = {};\n  let res = 0;\n\n  for(let ele of nums) {\n    dp[ele] = 0;\n    dp[ele+1] = 0;\n    dp[ele+2] = 0;\n  }\n\n  for(let i=nums.length-1; i>=0 ; i--) {\n    dp[nums[i]] = Math.max(dp[nums[i]], 1 + dp[nums[i] + 1]);\n    dp[nums[i]+1] = Math.max(dp[nums[i]+1], 1 + dp[nums[i] + 2]);\n  }\n\n  for(let key of Object.keys(dp)) {\n    res = Math.max(res, dp[key]);\n  }\n\n  return res;\n}\n```
['JavaScript']