Easy and fast C++ Solution with full explanation
Intuition\nsorting the potions array to reduce the number of operations to search(binary search) for the element whose product with ith spell is greater than or
# Intuition\nsorting the potions array to reduce the number of operations to search(binary search) for the element whose product with ith spell is greater than or equal to the success.\n\n# Approach\n1. sort `potions` array.\n2. use `binary search` to search for the element whose product with that spell is equal or just greater than `success`.\n3. count all the elements after that element *(including that element)* in `potions` array as all the elements after this element are greater than it and hence will result in greater product than `success`.\n4. store that count in an `ans` vector.\n5. return `ans`.\n\n# Complexity\n- Time complexity:\n`O(n*logm)`\n\n- Space complexity:\n`O(n)`\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {\n        int n = spells.size();\n        int m = potions.size();\n        sort(potions.begin(), potions.end());\n        vector<int> ans;\n        for(auto &i: spells){\n            int j = 0;\n            int k = m-1;\n            while(j<=k){\n                int mid = (j+k)/2;\n                int long x = potions[mid];\n                if(i*x<success){\n                    j = mid + 1;\n                }\n                else{\n                    k = mid - 1;\n                }\n            }\n            ans.push_back(m-k-1);\n        }\n        return ans;\n    }\n};\n```
['Array', 'Binary Search', 'Sorting', 'C++']