Program to find a list of product of all elements except the current index in Python

Given a list of numbers, we need to find a new list where each element at index i is the product of all numbers in the original list except the one at index i. This problem must be solved without using division.

So, if the input is nums = [2, 3, 4, 5, 6], then the output will be [360, 240, 180, 144, 120].

Approach

We use two auxiliary arrays to store left and right products:

  • Left array: Contains products of all elements to the left of each index
  • Right array: Contains products of all elements to the right of each index
  • Final result: Multiply corresponding elements from left and right arrays

Algorithm Steps

  • If size of nums < 1, return nums
  • Create left and right arrays of same size as nums
  • Fill left array with products of elements to the left of each index
  • Fill right array with products of elements to the right of each index
  • Multiply corresponding elements from left and right arrays

Example

class Solution:
    def solve(self, nums):
        if len(nums) < 1:
            return nums
        
        l = len(nums)
        left = [None] * l
        right = [None] * l
        temp = 1
        
        # Fill left array
        for i in range(len(nums)):
            if i == 0:
                left[i] = temp
            else:
                temp = temp * nums[i - 1]
                left[i] = temp
        
        # Fill right array
        temp = 1
        for i in range(len(nums) - 1, -1, -1):
            if i == len(nums) - 1:
                right[i] = temp
            else:
                temp = temp * nums[i + 1]
                right[i] = temp
        
        # Multiply left and right products
        for i in range(len(nums)):
            left[i] = left[i] * right[i]
        
        return left

# Test the solution
ob = Solution()
nums = [2, 3, 4, 5, 6]
result = ob.solve(nums)
print("Input:", nums)
print("Output:", result)
Input: [2, 3, 4, 5, 6]
Output: [360, 240, 180, 144, 120]

How It Works

Let's trace through the example [2, 3, 4, 5, 6]:

  • Left products: [1, 2, 6, 24, 120] (products of elements to the left)
  • Right products: [360, 120, 30, 6, 1] (products of elements to the right)
  • Final result: [1×360, 2×120, 6×30, 24×6, 120×1] = [360, 240, 180, 144, 120]

Alternative Approach

Here's a more space-efficient version using only one extra array:

def product_except_self(nums):
    n = len(nums)
    result = [1] * n
    
    # Store left products in result array
    for i in range(1, n):
        result[i] = result[i-1] * nums[i-1]
    
    # Multiply with right products on the fly
    right = 1
    for i in range(n-1, -1, -1):
        result[i] *= right
        right *= nums[i]
    
    return result

# Test the optimized solution
nums = [2, 3, 4, 5, 6]
result = product_except_self(nums)
print("Input:", nums)
print("Output:", result)
Input: [2, 3, 4, 5, 6]
Output: [360, 240, 180, 144, 120]

Conclusion

The two-pass approach effectively solves the product array problem without division by using left and right product arrays. The optimized version reduces space complexity by using only one extra array while maintaining O(n) time complexity.

Updated on: 2026-03-25T11:29:08+05:30

779 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements