JavaScript, Java, C  and Python solution with time complexity of O(n log m)
\n\n# Approach\nSort the potions array in descending order.\nCreate an empty hash map map to store the results.\nFor each element in the spells array:\na. If th
\n\n# Approach\nSort the potions array in descending order.\nCreate an empty hash map map to store the results.\nFor each element in the spells array:\na. If the element is not in the map, calculate the value of success / spells[i] and find the position of the first element in the sorted potions array that is greater than or equal to this value using a binary search. Store this position in len and add it to the res array. Also store len in the map against spells[i].\nb. If the element is already in the map, retrieve the value of len from the map and add it to the res array.\nReturn the res array.\n\n# Complexity\n- Time complexity: O(n log m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(m + n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# JavaCsript\n```\n/**\n * @param {number[]} spells\n * @param {number[]} potions\n * @param {number} success\n * @return {number[]}\n */\n var successfulPairs = function(spells, potions, success) {\n    let res = [];\n    potions.sort((a, b) => b-a);\n    let map = new Map();\n    \n    for(let i=0; i<spells.length; i++){\n        if(!map.has(spells[i])){\n            let s = success / spells[i];\n            let len = search(potions, s);\n            res.push(len);\n            map.set(spells[i], len);\n        }else{\n            let len = map.get(spells[i]);\n            res.push(len);\n        }\n    }\n    \n    return res;\n};\n\nfunction search(potions, target){\n    let res = 0;\n    let left = 0;\n    let right = potions.length-1;\n    while(left <= right){\n        let mid = Math.floor((left + right) / 2);\n        if(potions[mid] < target){\n            right = mid - 1;\n        }else{\n            left = mid + 1;\n            res = mid + 1;\n        }\n    }\n\n    return res;\n}\n```\n# Java\n```\npublic class SuccessfulPairs {\n    public static int[] successfulPairs(int[] spells, int[] potions, int success) {\n        ArrayList<Integer> res = new ArrayList<>();\n        int[] sortedPotions = potions.clone();\n        Arrays.sort(sortedPotions);\n        Map<Integer, Integer> map = new HashMap<>();\n\n        for(int i=0; i<spells.length; i++){\n            if(!map.containsKey(spells[i])){\n                int s = success / spells[i];\n                int len = search(sortedPotions, s);\n                res.add(len);\n                map.put(spells[i], len);\n            }else{\n                int len = map.get(spells[i]);\n                res.add(len);\n            }\n        }\n\n        int[] result = new int[res.size()];\n        for(int i=0; i<res.size(); i++){\n            result[i] = res.get(i);\n        }\n        return result;\n    }\n\n    public static int search(int[] potions, int target){\n        int res = 0;\n        int left = 0;\n        int right = potions.length-1;\n        while(left <= right){\n            int mid = (left + right) / 2;\n            if(potions[mid] < target){\n                right = mid - 1;\n            }else{\n                left = mid + 1;\n                res = mid + 1;\n            }\n        }\n\n        return res;\n    }\n}\n```\n# C\n```\n\n\nint search(int* potions, int potionsSize, int target){\n    int res = 0;\n    int left = 0;\n    int right = potionsSize-1;\n    while(left <= right){\n        int mid = (left + right) / 2;\n        if(potions[mid] < target){\n            right = mid - 1;\n        }else{\n            left = mid + 1;\n            res = mid + 1;\n        }\n    }\n\n    return res;\n}\n\nint* successfulPairs(int* spells, int spellsSize, int* potions, int potionsSize, int success, int* returnSize) {\n    int* res = malloc(spellsSize * sizeof(int));\n    int* sortedPotions = malloc(potionsSize * sizeof(int));\n    memcpy(sortedPotions, potions, potionsSize * sizeof(int));\n    qsort(sortedPotions, potionsSize, sizeof(int), (const void *)compare);\n    int* map = calloc(spellsSize, sizeof(int));\n\n    for(int i=0; i<spellsSize; i++){\n        if(map[spells[i]] == 0){\n            int s = success / spells[i];\n            int len = search(sortedPotions, potionsSize, s);\n            res[i] = len;\n            map[spells[i]] = len;\n        }else{\n            int len = map[spells[i]];\n            res[i] = len;\n        }\n    }\n    \n    free(map);\n    free(sortedPotions);\n    *returnSize = spellsSize;\n    return res;\n}\n\nint compare(const void * a, const void * b) {\n   return (*(int*)b - *(int*)a);\n}\n```\n# Python\n```\nfrom typing import List\nimport bisect\n\ndef successfulPairs(spells: List[int], potions: List[int], success: int) -> List[int]:\n    res = []\n    sortedPotions = sorted(potions, reverse=True)\n    map = {}\n\n    for i in range(len(spells)):\n        if spells[i] not in map:\n            s = success / spells[i]\n            len = bisect.bisect_left(sortedPotions, s)\n            res.append(len)\n            map[spells[i]] = len\n        else:\n            len = map[spells[i]]\n            res.append(len)\n\n    return res\n```
['C', 'Python', 'Java', 'JavaScript']