Easy Solution :)
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```\nclass Solution {\npublic:\n    int f(int n, const vector<int>& v, int i, int diff,vector<vector<int>>&dp) {\n        if (i >= n || diff > 1)\n            return 0;\n        if (dp[i][diff + 2] != -1)\n            return dp[i][diff + 2];\n        int val = 0;\n        if (diff == -2) {\n            if (i != n - 1)\n                val = max(f(n, v, i + 1, diff,dp),\n                           1+max( f(n, v, i + 1, v[i + 1] - v[i],dp),\n                            f(n, v, i + 1, v[i + 1] - v[i] - 1,dp)));\n        } else {\n            if (diff == 0) {\n                val = 1 + f(n, v, i + 1, (i != n - 1 ? v[i + 1] : 0) - v[i] - 1,dp);\n            } else if (diff == 1) {\n                val = 1 + f(n, v, i + 1, (i != n - 1 ? v[i + 1] : 0) - v[i],dp);\n            } else if (diff == -1) {\n                val = f(n, v, i + 1, diff + (i != n - 1 ? v[i + 1] : 0) - v[i],dp);\n            }\n        }\n        return dp[i][diff + 2] = val;\n    }\n    int maxSelectedElements(vector<int>& nums) {\n        int n = nums.size(); \n        if (n == 1)\n            return 1;\n        sort(nums.begin(), nums.end());\n        vector<vector<int>> dp(n,vector<int>(4,-1));\n        return f(n, nums, 0, -2,dp);\n    }\n};\n\n```
['C++']