Beats 100% with dp and logic commented
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 {\n    //opt -> 0 means prev index element was not incremented and added in series\n    //opt -> 1 means prev index element was  incremented and  added\n    int solve(int idx , int prev , int opt , int[] nums , int dp[][]){\n        if(idx == nums.length) return 0  ;\n\n        int t1 = 0 ; \n        int t2 = 0 ;\n        int t3 = 0 ; \n        \n        if(dp[prev+1][opt] != -1) return dp[prev+1][opt] ; \n        if(prev == -1){\n            t1 = 1 + solve(idx+1 , idx , 0 , nums , dp) ; \n            t2 = 1 + solve(idx+1 , idx , 1 , nums , dp) ; \n            t3 = solve(idx+1 , -1 , opt , nums , dp) ; \n        }\n        else{\n            if(opt == 1){\n                if(nums[prev]+1 >nums[idx]){\n                    t3 = solve(idx+1 , prev , 1 , nums , dp) ; \n                }\n                else if(nums[prev]+1 == nums[idx]){\n                    t2 = 1 + solve(idx+1 , idx , 1 , nums , dp) ; \n                    t3 = solve(idx+1 , prev , opt , nums , dp) ; \n                }\n                else if(nums[prev] + 2 == nums[idx]){\n                    t1 = 1 + solve(idx+1 , idx , 0 , nums , dp) ;\n                }\n                else if( nums[idx]> nums[prev]+1){\n                    return dp[prev+1][opt] =  0 ; \n                }\n            }\n            else{\n                if(nums[prev] == nums[idx]){\n                    t2 = 1 + solve(idx+1 , idx , 1 , nums , dp) ; \n                }\n                else if(nums[prev] + 1 == nums[idx]){\n                    t1 = 1 + solve(idx+1 , idx , 0 , nums , dp) ; \n                }\n                else if(nums[prev]+1 < nums[idx]){\n                    return dp[prev+1][opt] =  0 ;\n                }\n            }\n        }\n\n        return dp[prev+1][opt] = Math.max(t1 , Math.max(t2 , t3)) ; \n    }\n    public int maxSelectedElements(int[] nums) {\n        Arrays.sort(nums) ;\n\n        int dp[][] = new int[nums.length+2][2] ; \n\n        for(int[] I : dp) Arrays.fill(I,-1) ;  \n\n        return solve(0 , -1 , 0 , nums , dp) ; \n    }\n}\n```
['Java']