Skip to content

Instantly share code, notes, and snippets.

@ksamirdev
Created April 5, 2026 04:28
Show Gist options
  • Select an option

  • Save ksamirdev/588acd949b30490939036d14d0fabb6d to your computer and use it in GitHub Desktop.

Select an option

Save ksamirdev/588acd949b30490939036d14d0fabb6d to your computer and use it in GitHub Desktop.
# Thanks to Greg Hogg for an amazing explaination
# Video: https://www.youtube.com/watch?v=yKZFurr4GQA
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
l_mult = 1
r_mult = 1
n = len(nums)
l_arr = [0] * n
r_arr = [0] * n
for i in range(n):
j = -i - 1
l_arr[i] = l_mult
r_arr[j] = r_mult
l_mult *= nums[i]
r_mult *= nums[j]
return [l*r for l, r in zip(l_arr, r_arr)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment