✅ EASY || 💯 BEATS-PROOF || 🌟 C++ || 🧑‍💻 BEGINNER FRIENDLY || 🙂 DETAILED EXPLANATION
\n\n# \uD83C\uDF1F PROOF\n---\n\n\n---\n\n\n\n# \uD83C\uDF1F Intuition\n---\n Describe your first thoughts on how to solve this problem. \n#### The problem requ
```\n```\n# \uD83C\uDF1F PROOF\n---\n![image.png](https://assets.leetcode.com/users/images/d2fc3810-bcb7-4736-897b-eff29d94a1aa_1732099826.5885377.png)\n\n---\n```\n```\n\n# \uD83C\uDF1F Intuition\n---\n<!-- Describe your first thoughts on how to solve this problem. -->\n#### The problem requires counting the number of potions that can form a "successful" pair with each spell. Instead of brute force, which would be inefficient due to the constraints, we optimize using:\n\n1. **Sorting and binary search, or**\n2. **A frequency array approach to preprocess the count of successful potions.**\n\n#### Here, the code utilizes a frequency array to calculate the number of potions greater than or equal to a required value efficiently.\n```\n```\n# \uD83C\uDF1F Approach\n---\n<!-- Describe your approach to solving the problem. -->\n1. **Preprocessing Potions**:\n\n    - Use an array `postfix[]` where `postfix[i]` represents the count of potions with strength greater than or equal to `i`.\n    - Traverse the potions array to update counts in `postfix[]`.\nProcess `postfix[]` in reverse to calculate cumulative frequencies.\n\n2. **Processing Spells**:\n\n    - For each spell, calculate the minimum potion strength (`val`) required for a successful pair:\n        - `val = ceil(success / spell)`.\n        - Efficiently handle the division to avoid floating-point issues using `(success + spell - 1) / spell`.\n    - Retrieve the count of potions greater than or equal to `val` from `postfix[]`.\n\n3. **Update Spells In-place**:\n\n    - Modify the `spells` array to store the result directly, avoiding additional space for the output.\n\n\n```\n```\n# \uD83C\uDF1F Complexity\n---\n- ### Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- **O(n + m + maxPotionStrength):**\n- **O(m) for updating `postfix`[].**\n- **O(maxPotionStrength) for cumulative counts.**\n- **O(n) for iterating over `spells`.**\n---\n- ### Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n**O(maxPotionStrength) for the `postfix[]` array.**\n```\n```\n# \uD83C\uDF1F Code\n---\n```cpp []\nclass Solution {\npublic:\n    vector<int> successfulPairs(vector<int>& spells, vector<int>& potions,\n                                long long success) {\n        int max_val = 0;\n\n        for (auto& potion : potions)\n            max_val = max(max_val, potion);\n\n        int postfix[max_val + 1];\n\n        memset(postfix, 0, sizeof(postfix));\n\n        for (auto& potion : potions)\n            postfix[potion]++;\n\n        for (int i = max_val - 1; i >= 0; --i)\n            postfix[i] += postfix[i + 1];\n\n        for (int i = 0; i < spells.size(); ++i) {\n            long long val = success / (long long)spells[i];\n\n            if (success % (long long)spells[i] != 0)\n                val++;\n\n            spells[i] = val <= max_val ? postfix[val] : 0;\n        }\n\n        return spells;\n    }\n};\n```\n\n```\n```\n---\n\n![upvote.png](https://assets.leetcode.com/users/images/32a51133-5080-489b-90ce-12a6ff5d445f_1732099953.4097476.png)\n\n---\n```\n```\n
['Array', 'Two Pointers', 'Binary Search', 'C++']