Leetcode 238. Product of Array Except Self | Hindi | Python | Leetcode | Optimized way

Published: 06 December 2025
on channel: DataEngineering360
233
6

#leetcode #python #timecomplexity
Q. Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

You must write an algorithm that runs in O(n) time and without using the division operation.
Example 1:

Input: nums = [1,2,3,4]
Output: [24,12,8,6]


Solution -
Optimized way - Note - this is sample code.. we cannot use angle brackets in description, so I removed them from the code.

class Solution:
def productExceptSelf(self, nums: List[int]) - List[int]:

ls = [1]*len(nums)
rs = [1]*len(nums)
f = [1]*len(nums)

for i in range(1,len(nums)):
ls[i] = ls[i-1]* nums[i-1]

for j in range(len(nums)-2,-1,-1):
rs[j] = rs[j+1]*nums[j+1]

for k in range(len(nums)):
f[k] = ls[k]* rs[k]

return f



#python #leetcodesolutions #dsa #pythoninterviewquestions

Python Developer Interview Questions Answers
Top 10 Python Developer Interview Questions Answer
Mostly asked Python Developer Interview Questions Answer
Commonly asked Python Developer Interview Questions Answer



Tags -
Python leetcode problems
leetcode two sum
leetcode solutions
leetcode hindi
two sum python
two sum problem


On this page of the site you can watch the video online Leetcode 238. Product of Array Except Self | Hindi | Python | Leetcode | Optimized way with a duration of online in good quality, which was uploaded by the user DataEngineering360 06 December 2025, share the link with friends and acquaintances, this video has already been watched 233 times on youtube and it was liked by 6 viewers. Enjoy your viewing!