C++ | Clean | Easy ✅
\u2764 Please Upvote if you find helpful \u2764\n\nExplanation (Step by Step)\n\nStep 1. \nThe function starts by initializing the vector v with zeros and then 
\u2764 Please Upvote if you find helpful \u2764\n\n**Explanation (Step by Step)**\n\n**Step 1.** \nThe function starts by initializing the vector `v` with `zeros` and then `sorting` the vector `p` in `non-decreasing` order.\n\n**Step 2.** \nFor each element `s[i]` in vector `s`, a `binary search` is performed on vector `p` to find the index of the `largest` element whose product with `s[i]` is `less than eqaul` to `suc`. The binary search is performed by maintaining two indices `l` and `h` representing the lower and upper bounds of the search range, respectively. The search continues until `l` is `less than equal` to `h`.\n\n**Step 3** \nInside the loop, the middle index `mid` is computed as the average of `l` and `h`. If the product of `s[i]` and `p[mid]` is greater than or equal to suc, the upper bound `h` is adjusted to search the `left half` of the array, otherwise the lower bound `l` is adjusted to search the `right half` of the array.\n\n**Step 4**\nAfter the binary search, the number of successful pairs for the element `s[i]` is calculated by `subtracting` the `index of the last element` found in the search from the `size of p`. This is because all elements in `p` starting from this index `satisfy` the condition for a successful pair.\n\n**Step 5**\nFinally, the vector `v` is returned, containing the number of `successful pairs` for each element in `s`. \n\n**FAQ** : `Why binary search is used ?` \n=> By using binary search, the code avoids the need to compare the current element in s with all elements in p, which would have a time complexity of $$O(n\xB2)$$. Instead, the binary search algorithm efficiently reduces the search space by dividing it in half at each step, resulting in a much faster search time $$O(nlog(n))$$.\n\n```\nclass Solution {\npublic:\n    vector<int> successfulPairs(vector<int>& s, vector<int>& p, long long suc) {\n        \n        vector<int> v(s.size(),0);\n        sort(p.begin(),p.end());\n        \n        for(int i=0;i<s.size();i++)\n        {\n            int h=p.size()-1;\n            int l=0;\n            int mid;\n            while(l<=h)\n            {\n                mid = l + (h-l)/2;\n                \n                if((long long int)s[i]*(long long int)p[mid] >= suc)\n                    h = mid-1;\n                \n                else\n                    l = mid+1;\n            }\n            \n            v[i] = p.size()-1-h;\n        }\n        \n        return v;\n    }\n};\n```\n![IMG_20230402_115743.jpg](https://assets.leetcode.com/users/images/4d144a01-19ce-4def-8200-2c2ca9987f29_1680416869.6585193.jpeg)\n  \u2227,,,\u2227\n(  \u0333\u2022 \xB7 \u2022 \u0333)\n/    \u3065\u2661\n
['Binary Search', 'C', 'C++']