C++ | EASY EXPLANATION | BEGINNER FRIENDLY | WITHOUT BS🙂
Intuition\nWE WILL SORT SPELLS IN DESCENDING ORDER AND POTIONS IN ASCENDING ORDER . SO THAT EVERY TIME I CHECK FOR A GIVEN SPELL ,I DON\'T HAVE TO TRAVERSE FROM
# Intuition\nWE WILL SORT SPELLS IN DESCENDING ORDER AND POTIONS IN ASCENDING ORDER . SO THAT EVERY TIME I CHECK FOR A GIVEN SPELL ,I DON\'T HAVE TO TRAVERSE FROM BEGINNING . It will start from some index k .\n\nAND ALSO.\nIF ANY GREATER SPELL IS NOT ABLE TO MAKE SUCCESS FROM POTIONS ,THEN \nNO SPELL AFTER THAT WILL GIVE ANY SUCCESS . THEREFORE \n\n```\nif(flag==0)break;\n```\n\n# Approach\nSEE CODE WITH COMMENTS . YOU WILL SURELY GET IT .\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> successfulPairs(vector<int>& ss, vector<int>& p, long long suc) {\n        vector<int>s=ss;// TO MAKE USE OF ITS SORTED(DESCENDING) FORM .\n        unordered_map<int,int>mpp;// TO STORE SPELL WITH THEIR SUCCESS.\n        vector<int>ans; // FINAL RESULT WILL BE STORED .\n\n        sort(p.begin(),p.end());\n\n        sort(s.begin(),s.end(),greater<int>()); // SORTING IN DESCENDING .\n\n        int n=s.size();int n2=p.size();\n // K IS USED FOR NEW INDEX FROM WHERE NEXT SPELL SHOULD CHECK IN POTION FOR THEIR SUCCESS .\n        int k=0;\n\n        for(int i=0;i<n;i++){\n\n            bool flag=0;// for check if any spell is making success or not.\n\n            for(int j=k;j<n2;j++){\n                // 1LL for avoiding overflow of int .\n                if(1LL*s[i]*p[j]>=suc){\n                    k=j; // storing new index for next spell to traverse.\n                    flag=1;\n                    mpp[s[i]]=n2-j;// storing success of current spell.\n                    break;\n                }\n            }\n            // if spell is not making success ,break;\n            if(flag==0){  break; } \n        }\n\n        for(int i=0;i<n;i++){\n            // if spell was making any success .It had stored in mpp.\n            if(mpp.count(ss[i]))ans.push_back(mpp[ss[i]]);\n\n            else ans.push_back(0);\n        }\n\n        return ans;\n    }\n};\n```\nCODE BY :) AMAN MAURYA \nTHANK YOU \n\nUPVOTE IF YOU FOUND IT EASY OR DIFFERENT .\n![UPVOTE.jpeg](https://assets.leetcode.com/users/images/920b2dc1-352a-4f35-a497-d3016f8a562a_1680415841.5855615.jpeg)\n
['C++']