Program to return number of smaller elements at right of the given list in Python

In Python, a list is an ordered and mutable data structure where elements are enclosed in square brackets []. In some scenarios, we may need to count how many elements to the right of each element in the list are smaller than it.

For example, given the list [5, 2, 6, 1], for element 5 at index 0, there are 2 smaller elements (2 and 1) to its right. For element 2 at index 1, there is 1 smaller element (1) to its right.

Using Binary Search with bisect_left() Function

The bisect_left() function from the bisect module is used to locate the insertion point for an element in a sorted list to maintain the list's sorted order. The insertion point returned by bisect_left() is the index where the element can be inserted without violating the sort order by placing it before any existing entries with the same value.

Example 1

Following is the example in which we traverse the input list from right to left, using the bisect_left() function to insert elements into a sorted list and track the position of insertion, which represents the count of smaller elements ?

import bisect

def count_smaller_elements(arr):
    result = []
    sorted_list = []
    
    for num in reversed(arr):
        index = bisect.bisect_left(sorted_list, num)
        result.append(index)
        bisect.insort(sorted_list, num)
    
    return result[::-1]

# Sample input list
numbers = [5, 2, 6, 1]
print("Count of smaller elements to the right:", count_smaller_elements(numbers))

The output of the above code is ?

Count of smaller elements to the right: [2, 1, 1, 0]

Example 2

Here is another example using a class-based approach with the bisect_left() function ?

import bisect

class Solution:
    def solve(self, nums):
        result = []
        increasing_list = []
        
        while nums:
            num = nums.pop()
            result.append(bisect.bisect_left(increasing_list, num))
            bisect.insort(increasing_list, num)
        
        return result[::-1]

# Create solution object and test
solution = Solution()
numbers = [4, 5, 9, 7, 2]
print("Smaller elements count:", solution.solve(numbers))

The output of the above code is ?

Smaller elements count: [1, 1, 2, 1, 0]

How It Works

The algorithm works by processing the list from right to left:

  1. Start with an empty sorted list
  2. For each element from right to left, use bisect_left() to find where it would be inserted in the sorted list
  3. This insertion index equals the count of smaller elements
  4. Insert the element into the sorted list using insort()
  5. Reverse the result to match the original order

Conclusion

The binary search approach using bisect_left() efficiently counts smaller elements to the right with O(n log n) time complexity. This method maintains a sorted list of processed elements and uses binary search for fast lookups.

Updated on: 2026-03-25T11:19:04+05:30

494 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements