Algorithm Improvement - element differences in list The task is, you have an array of n numbers called nums, and an array of m numbers called maxes.

For each element in maxes, find the number of elements for which m is bigger than or equal to n.

nums = [4, 2, 5, 10, 8]
maxes = [3, 1, 7, 8]

output = [1, 0, 3, 4]


Eg, The first element, 3, in maxes, is only higher than 2 in nums, hence the first element in output is 1

The initial solution I could think of was trivial, and its O(n^2)

def sol_naive(nums,maxes):
    output = []
    for entry in maxes:
        higher_numbers = len([x for x in nums if x &lt;= entry])
        output.append(higher_numbers)
    return output


Wondering if there's a better approach to solve this problem?
One simple observation is that this problem gets much easier if you sort nums O(n log(n). Then you can use bisect_left which uses binary search to find where an element would go in a sorted list and is O(log(n)) for each element in m. This yields a total time of O((n+m) log(n))

from bisect import bisect_left

def sol_sorted(nums,maxes):
    output = []
    nums.sort()
    for m in maxes:
        ind = bisect_left(nums, m)
        output.append(len(nums) - ind)
    return output