🔥 [LeetCode The Hard Way] 🔥 Explained Line By Line
\n\uD83D\uDD34 Check out LeetCode The Hard Way for more solution explanations and tutorials. \n\uD83D\uDFE0 Check out our Discord Study Group for live discussio
\n\uD83D\uDD34 Check out [LeetCode The Hard Way](https://wingkwong.github.io/leetcode-the-hard-way/) for more solution explanations and tutorials. \n\uD83D\uDFE0 Check out our [Discord Study Group](https://discord.gg/Nqm4jJcyBf) for live discussion.\n\uD83D\uDFE2 Give a star on [Github Repository](https://github.com/wingkwong/leetcode-the-hard-way) and upvote this post if you like it.\n\uD83D\uDD35 Check out [YouTube Channel](https://www.youtube.com/@leetcodethehardway) if you are interested.\n\n```cpp\nclass Solution {\npublic:\n    int maxCount(vector<int>& banned, int n, int maxSum) {\n        // put all banned elements to a set\n        set<int> s(banned.begin(), banned.end());\n        int sum = 0, ans = 0;\n        // try each possible number (rule #1 & #2)\n        for (int i = 1; i <= n; i++) {\n            // if the number `i` is banned, then we skip it (rule #3)\n            if (s.count(i)) continue;\n            // we can include this number as long as it doesn\'t exceed `maxSum` (rule #4)\n            if (sum + i > maxSum) break;\n            // add to current sum\n            sum += i;\n            // increase ans by 1 as we choose number `i`\n            ans += 1;\n        }\n        return ans;\n    }\n};\n```
['C++']