Simple and Easy Solution | Brute Force Approach | ✅Beats 100% | C++| Java | Python | JavaScript
\n# \u2B06\uFE0FUpvote if it helps \u2B06\uFE0F\n## Connect with me on Linkedin Bijoy Sing.\n\n## Follow me also on Codeforces:Bijoy Sing\n\n###### Solution in 
\n# \u2B06\uFE0FUpvote if it helps \u2B06\uFE0F\n## Connect with me on Linkedin [Bijoy Sing](https://www.linkedin.com/in/bijoy-sing-236a5a1b2/).\n\n## Follow me also on Codeforces:[Bijoy Sing](https://codeforces.com/profile/BijoySingh7)\n\n###### *Solution in C++, Python, Java, and JavaScript*\n\n```cpp []\nclass Solution {\npublic:\n    int maxCount(vector<int>& banned, int n, int maxSum) {\n        set<int> st(banned.begin(), banned.end()); \n        int sum = 0, count = 0;\n\n        for (int i = 1; i <= n; i++) {\n            if (st.count(i) == 0 && sum + i <= maxSum) { \n                sum += i;\n                count++;\n            }\n        }\n        return count;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def maxCount(self, banned: List[int], n: int, maxSum: int) -> int:\n        banned_set = set(banned)  \n        total_sum = 0\n        count = 0\n\n        for i in range(1, n + 1):\n            if i not in banned_set and total_sum + i <= maxSum:  # Skip banned numbers\n                total_sum += i\n                count += 1\n\n        return count\n```\n\n```java []\nclass Solution {\n    public int maxCount(int[] banned, int n, int maxSum) {\n        Set<Integer> bannedSet = new HashSet<>();\n        for (int num : banned) {\n            bannedSet.add(num);\n        }\n        \n        int sum = 0, count = 0;\n        for (int i = 1; i <= n; i++) {\n            if (!bannedSet.contains(i) && sum + i <= maxSum) { // Skip banned numbers\n                sum += i;\n                count++;\n            }\n        }\n\n        return count;\n    }\n}\n```\n\n```javascript []\nclass Solution {\n    maxCount(banned, n, maxSum) {\n        const bannedSet = new Set(banned); // Use a set for faster lookups\n        let sum = 0, count = 0;\n\n        for (let i = 1; i <= n; i++) {\n            if (!bannedSet.has(i) && sum + i <= maxSum) { // Skip banned numbers\n                sum += i;\n                count++;\n            }\n        }\n\n        return count;\n    }\n}\n```\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThe problem can be solved efficiently by skipping the banned numbers and keeping track of the sum and count of non-banned numbers. Using a set for banned numbers allows quick lookups.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n1. Store all banned numbers in a set for O(1) average-case lookups.\n2. Iterate through numbers from 1 to `n`.\n3. If the number is not banned and does not exceed the `maxSum`, add it to the total sum and increment the count.\n4. Return the count of such numbers.\n\n# Complexity\n- Time complexity: $$O(n + b)$$, where $$n$$ is the upper limit of numbers, and $$b$$ is the size of the banned list.\n- Space complexity: $$O(b)$$, for storing the banned numbers in a set.\n\n### *If you have any questions or need further clarification, feel free to drop a comment! \uD83D\uDE0A*\n```
['Array', 'Hash Table', 'Greedy', 'Sorting', 'Ordered Set', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']