Image Explanation🏆- [Binary Search with & without Inbuild Libraries] - C++/Java/Python
Video Solution (Aryan Mittal) - Link in LeetCode Profile\nSuccessful Pairs of Spells and Potions by Aryan Mittal\n\n\n# Approach & Intution\n\n\n\n\n\n\n\n\n\n\
# Video Solution (`Aryan Mittal`) - Link in LeetCode Profile\n`Successful Pairs of Spells and Potions` by `Aryan Mittal`\n![thumb.png](https://assets.leetcode.com/users/images/5857de7f-4a28-4d73-80e0-117e42178bf3_1680402420.5635715.png)\n\n# Approach & Intution\n![image.png](https://assets.leetcode.com/users/images/99a332e2-1f9b-42b1-9f38-d328ba555a0d_1680397450.5684943.png)\n![image.png](https://assets.leetcode.com/users/images/95fd5621-491c-49ec-b02a-b7fb09792299_1680397462.2408035.png)\n![image.png](https://assets.leetcode.com/users/images/5db5d26c-ddce-4df1-8bac-d09eba7231fa_1680397482.1739225.png)\n![image.png](https://assets.leetcode.com/users/images/e0c85a7d-6300-4e09-99bc-7501984344ef_1680397494.6180933.png)\n![image.png](https://assets.leetcode.com/users/images/f420160f-7384-4baf-991b-44f7a5b79dfc_1680397503.9567719.png)\n![image.png](https://assets.leetcode.com/users/images/fb798d7d-2669-44d6-ac56-3af7fc5eec20_1680397511.739848.png)\n![image.png](https://assets.leetcode.com/users/images/55723900-7dea-43b9-bbf5-8a183d663fd7_1680397521.3559468.png)\n![image.png](https://assets.leetcode.com/users/images/39a8bc71-7920-4397-a37a-c6fa82d3b416_1680398553.798697.png)\n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {\n        sort(potions.begin(), potions.end());\n        vector<int> res;\n        for (int spell: spells) {\n            long potion_needed = (success + spell - 1) / spell;\n            auto it = lower_bound(potions.begin(), potions.end(), potion_needed);\n            res.push_back(potions.end() - it);\n        }\n        return res;\n    }\n};\n```\n```Java []\nclass Solution {\n    public int[] successfulPairs(int[] spells, int[] potions, long success) {\n\t\tArrays.sort(potions);\n\t\tTreeMap<Long, Integer> map = new TreeMap<>();\n        map.put(Long.MAX_VALUE, potions.length);\n        \n\t\tfor (int i = potions.length - 1; i >= 0; i--) {\n\t\t\tmap.put((long) potions[i], i);\n\t\t}\n\t\tfor (int i = 0; i < spells.length; i++) {\n            long need = (success + spells[i] - 1) / spells[i];\n\t\t\tspells[i] = potions.length - map.ceilingEntry(need).getValue();\n\t\t}\n\t\treturn spells;\n\t}\n}\n```\n```Python []\nclass Solution:\n    def successfulPairs(self, spells, potions, success):\n        sorted_potions = sorted(potions)\n        result = []\n        for a in spells:\n            count = len(sorted_potions) - bisect_left(sorted_potions, (success + a - 1) // a)\n            result.append(count)\n        return result\n```\n\n\n# Code Without Inbuild Libraries\nExact same [Generalized Binary Search code](https://leetcode.com/problems/binary-search/solutions/3363888/image-explanation-most-generalized-binary-search-cjavapython/) discussed for C++/Java/Python.\n```C++ []\nclass Solution {\npublic:\n    int valid_pos(vector<int>& potions, long long success, int spell){\n        long potion_needed = (success + spell - 1) / spell;\n\n        int l=0,r=potions.size();\n        while(l<r){\n            int mid = l + (r-l)/2;\n            if(potions[mid] >= potion_needed)\n                r = mid;\n            else\n                l = mid + 1;\n        }\n        return l;\n    }\n\n    vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {\n        sort(potions.begin(), potions.end());\n        vector<int> res;\n        for (int spell: spells) {\n            res.push_back(potions.size()- valid_pos(potions, success, spell));\n        }\n        return res;\n    }\n};\n```\n```Java []\nclass Solution {\n    public int valid_pos(int[] potions, long success, int spell) {\n        long potion_needed = (success + spell - 1) / spell;\n        int l = 0, r = potions.length;\n        while (l < r) {\n            int mid = l + (r - l) / 2;\n            if (potions[mid] >= potion_needed)\n                r = mid;\n            else\n                l = mid + 1;\n        }\n        return l;\n    }\n\n    public int[] successfulPairs(int[] spells, int[] potions, long success) {\n        Arrays.sort(potions);\n        int[] res = new int[spells.length];\n        for (int i = 0; i < spells.length; i++) {\n            res[i] = potions.length - valid_pos(potions, success, spells[i]);\n        }\n        return res;\n    }\n}\n```\n```Python []\nclass Solution:\n    def valid_pos(self, potions: List[int], success: int, spell: int) -> int:\n        potion_needed = (success + spell - 1) // spell\n        l, r = 0, len(potions)\n        while l < r:\n            mid = l + (r - l) // 2\n            if potions[mid] >= potion_needed:\n                r = mid\n            else:\n                l = mid + 1\n        return l\n\n    def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:\n        potions.sort()\n        res = []\n        for spell in spells:\n            res.append(len(potions) - self.valid_pos(potions, success, spell))\n        return res\n```
['Binary Search', 'C++', 'Java', 'Python3']