java easy solution || hashmap || dp
Complexity\n- Time complexity: O(nlogn)\n\n- Space complexity: O(n)\n\n# Code\n\nclass Solution {\n    public int maxSelectedElements(int[] nums) {\n        Arr
# Complexity\n- Time complexity: O(nlogn)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution {\n    public int maxSelectedElements(int[] nums) {\n        Arrays.sort(nums);\n        HashMap<Integer,Integer> consecutiveCount = new HashMap<>();\n        int ans = 1;\n        for(int i = 0; i < nums.length; i++){\n            int c1 = consecutiveCount.getOrDefault(nums[i],0)+1;\n            int c2 = consecutiveCount.getOrDefault(nums[i]-1,0)+1;\n            consecutiveCount.put(nums[i]+1,c1);\n            consecutiveCount.put(nums[i],c2);\n            ans = Math.max(ans,Math.max(c1,c2));\n        }\n        return ans;\n\n    }\n}\n```
['Java']