Very Easy DP transition
\n# Code\nWith and without comments\n\nJava []\nclass Solution {\n    public int maxSelectedElements(int[] arr) {\n        \n        Arrays.sort(arr); //importa
\n# Code\nWith and without comments\n\n```Java []\nclass Solution {\n    public int maxSelectedElements(int[] arr) {\n        \n        Arrays.sort(arr); //important\n        \n        /**\n            dp[i][0] represents maximum number of consecutive elements starting\n            from i\'th element of array when 1 is not added to the i\'th element\n            \n            dp[i][1] represents maximum number of consecutive elements starting\n            from i\'th element of array when 1 is added to the i\'th element\n        */\n        \n        int dp[][] = new int[arr.length][];\n        \n        for (int i = 0; i < dp.length; i++)\n            dp[i] = new int[]{1, 1};\n        \n        for (int i = arr.length - 2; i >= 0; i--) {\n            //there are 3 different cases for DP transition\n            \n            //current element + 1 = next element, example: 9, 10\n            if (arr[i] + 1 == arr[i + 1]) {\n                dp[i][0] = 1 + dp[i + 1][0];\n                dp[i][1] = 1 + dp[i + 1][1];\n            }\n            \n            \n            //current element + 2 = next element, example: 8, 10\n            if (arr[i] + 2 == arr[i + 1]) {\n                dp[i][1] = 1 + dp[i + 1][0];\n            }\n            \n            //current element = next element, example: 10, 10\n            if (arr[i] == arr[i + 1]) {\n                \n                dp[i][0] = 1 + dp[i + 1][1];\n                \n                //pay attention to following dp transitions\n                dp[i][0] = Math.max(dp[i][0], dp[i + 1][0]);\n                dp[i][1] = Math.max(dp[i][1], dp[i + 1][1]); \n            }\n        }\n        \n        //return the maximum dp[i][]\n        int result = 0;\n        for (int i = 0; i < dp.length; i++) {\n            result = Math.max(result, dp[i][0]);\n            result = Math.max(result, dp[i][1]);\n        }\n        return result;\n    }\n}\n```\n\n```Java []\nclass Solution {\n    public int maxSelectedElements(int[] arr) {\n        Arrays.sort(arr);\n        int dp[][] = new int[arr.length][];\n        for (int i = 0; i < dp.length; i++)\n            dp[i] = new int[]{1, 1};\n        \n        for (int i = arr.length - 2; i >= 0; i--) {\n            if (arr[i] + 1 == arr[i + 1]) {\n                dp[i][0] = 1 + dp[i + 1][0];\n                dp[i][1] = 1 + dp[i + 1][1];\n            }\n            if (arr[i] + 2 == arr[i + 1]) {\n                dp[i][1] = 1 + dp[i + 1][0];\n            }\n            if (arr[i] == arr[i + 1]) {\n                dp[i][0] = 1 + dp[i + 1][1];\n                dp[i][0] = Math.max(dp[i][0], dp[i + 1][0]);\n                dp[i][1] = Math.max(dp[i][1], dp[i + 1][1]); \n            }\n        }\n        \n        int result = 0;\n        for (int i = 0; i < dp.length; i++) {\n            result = Math.max(result, dp[i][0]);\n            result = Math.max(result, dp[i][1]);\n        }\n        return result;\n    }\n}\n```\n\n\n
['Java']