2300. Successful Pairs of Spells and Potions EASY 96.70% Fast
\n\n# Intuition\nThe task is to determine, for each spell, how many potions can be paired with it such that the product of the spell\'s power and the potion\'s 
![image.png](https://assets.leetcode.com/users/images/878275d4-9761-4972-8b7e-96739234eccd_1718735756.581724.png)\n\n# Intuition\nThe task is to determine, for each spell, how many potions can be paired with it such that the product of the spell\'s power and the potion\'s power meets or exceeds a given success threshold. By sorting the potions, we can efficiently use binary search to find the smallest potion that, when multiplied with the current spell, meets the threshold.\n# Approach\n**Sort the Potions:** Start by sorting the list of potion powers. This allows us to use binary search to quickly find the minimum potion power that can pair successfully with a given spell.\n\n**Binary Search for Each Spell:** For each spell in the list of spells, perform a binary search on the sorted potions to find the smallest index where the product of the spell\'s power and the potion\'s power is at least the success threshold.\n\n**Calculate Successful Pairs:** The number of successful pairs for a given spell is then the number of potions from the found index to the end of the potions list. This is computed as the total number of potions minus the found index.\n\n**Store Results:** Store the result for each spell in a result list, which is returned at the end.\n# Complexity\n- Time complexity: **O(n\u2217log(m))**\n- Space complexity: **O(n)**\n# Code\n```\nclass Solution {\npublic:\n    vector<int> successfulPairs(vector<int>& s, vector<int>& p, long long suc) {\n\n        sort(p.begin(),p.end());\n        vector<int>ans;\n        int m = p.size();\n        int n = s.size();\n        int idx = 0;\n        for(int i=0;i<n;i++)\n        {\n            int l=0;\n            int r=m-1;\n            int idx=m;\n            while(l<=r){\n                int mid = l+(r-l)/2;\n                if(static_cast<long long>(s[i])*p[mid] >= suc)\n                {\n                    r=mid-1;\n                    idx = mid;\n                }\n                else{\n                    l = mid+1;\n                }\n            }\n            ans.push_back(max(m-idx,0));\n        }\n        return ans;\n    }\n};\n```
['C++']