Easy to understand C++ solution | | Constant space complexity O(1)
\n\n# Approach\n Describe your approach to solving the problem. \n1. Sort the elements of banned array in ascending order.\n2. Check for each number from 1 to n
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort the elements of banned array in ascending order.\n2. Check for each number from 1 to n and if they are in banned array, we skip it. (Make sure to check for duplicate elements in banned as well or use a set instead, but it will increase the space complexity).\n3. Else add it to sum making sure it does not exceed maxSum and return the count of such numbers.\n\n# Complexity\n- Time complexity: O(n*logn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxCount(vector<int>& banned, int n, int maxSum) {\n        sort(banned.begin(), banned.end());\n\n        int sum = 0, i = 1, ct = 0, j = 0, bn = banned.size();\n\n        while(i <= n){\n            if(j < bn && banned[j] == i){\n                while(j < bn && banned[j] == i) j++;\n                i++;\n                continue;\n            }\n            if(sum + i > maxSum){\n                break;\n            }\n            sum += i;\n            ct++;\n            i++;\n        }   \n\n        return ct;\n    }\n};\n```
['Array', 'Sorting', 'C++']