Simple sorting [No DP]
Approach\n0. Be greedy, sort everthing.\n1. Try to make the longest sequence, that is just one big than previous (1,2,3, so on)\n2. If you have (1,1) and you ha
# Approach\n0. Be greedy, sort everthing.\n1. Try to make the longest sequence, that is just one big than previous (1,2,3, so on)\n2. If you have (1,1) and you have to make one of them as 2 to make a longer sequence, note that down, so that you can never increase them again\n3. Now if you face a situation where you are just 2 more than the last then, you are faced with a choice to increase all the elements before you. So do it, BUT remember if you have already increased a previous number to accomodate it, You MUST NOT count it again to make your larger sequence this time. \n4. That\'s it you should now have your answer, make sure to keep updating your answer from your temp answer, time to time.\n\n# Complexity\n- Time complexity: O(nlogn)\n\n- Space complexity: O(n)\n\n# Concise Code \n```\nint maxSelectedElements(vector<int>& nums) {\n        sort(nums.begin(),nums.end());\n        int n = nums.size(), ans = 1, x = 1, prv = nums[0], ki = 0;\n        vector<int> inc(n);\n        for(int i=1;i<n;i++) {\n            if(nums[i]==prv) {\n                prv=nums[i]+1;\n                inc[i]=1;\n                x++;\n            } else if(nums[i]<prv) {\n                inc[i]=1;\n            } else if(nums[i]==prv+1) {\n                x++;\n                prv=nums[i];\n            } else if(nums[i]>(prv+1)) {\n                if(nums[i]==(prv+2)) {\n                    int y = 0;\n                    for(int j = i-1;j>=ki;j--) {\n                        if(!inc[j]) {\n                            y++;\n                            inc[j]=1;\n                        }\n                        else break;\n                    }\n                    y++;\n                    ans = max(ans,x);\n                    x=y; ki=i;\n                    prv=nums[i];\n                } else {\n                    ans = max(x,ans);\n                    x=1; ki=i;\n                    prv=nums[i];\n                }\n            }\n        }\n        ans = max(x,ans);\n        return ans;\n    }\n```\n\n# Code with Explaination\n```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        // Sort the input vector\n        sort(nums.begin(), nums.end());\n\n        // Initialize variables ans is for answer, x for temporary answer, prv is previous value and ki is the index from where the current best sequence had started\n        int n = nums.size(), ans = 1, x = 1, prv = nums[0], ki = 0;\n        vector<int> inc(n);\n\n        // Iterate over the sorted vector\n        for(int i = 1; i < n; i++) {\n            if(nums[i] == prv) {\n                // If current number is equal to previous, then in that case we can increase the current value and get a newer longer sequence, eg : [1,1] increase second 1 and get 2, and change inc[i] to 1 to indicated that this number is already increased, so we don\'t increase it again\n                prv = nums[i] + 1;\n                inc[i] = 1;\n                x++;\n            } else if(nums[i] < prv) {\n                // If current number is less than previous, it means its previous occurence was already increased so it must increase too, so set inc[i] to 1 to indicate it\n                inc[i] = 1;\n            } else if(nums[i] == prv + 1) {\n                // If current number is one more than previous, increment count and update previous, since this can be our sequence, no problem\n                x++;\n                prv = nums[i];\n            } else if(nums[i] > (prv + 1)) {\n                // If current number is more than one more than previous (means it can\'t be sequence as such)\n                if(nums[i] == (prv + 2)) {\n                    // If current number is two more than previous\n                    int y = 0;\n                    // Iterate backwards from current index, until you reach the end of the position where last sequence had started or you reach a place which has already been increased once to accomodate it\n                    for(int j = i - 1; j >= ki; j--) {\n                        // If number is not increased, increment count and mark as increased, and break if you come across a number that is already increased once and can\'t be counted again\n                        if(!inc[j]) {\n                            y++;\n                            inc[j] = 1;\n                        } else break;\n                    }\n                    y++;\n                    // Update maximum count\n                    ans = max(ans, x);\n                    // Update count and previous\n                    x = y; ki = i;\n                    prv = nums[i];\n                } else {\n                    // If current number is more than two more than previous and i can\'t update it to be part of newer sequence anyhow. So i restart counting and\n                    // Update maximum answer and reset temp count\n                    ans = max(x, ans);\n                    x = 1; ki = i;\n                    prv = nums[i];\n                }\n            }\n        }\n        // Update maximum count and return the answer\n        ans = max(x, ans);\n        return ans;\n    }\n};\n```
['Array', 'Greedy', 'Sorting', 'C++']