From f55a5e0f0bcf9753847bbff58423b1ad261ae4ad Mon Sep 17 00:00:00 2001 From: Saurabh Kokate Date: Sat, 4 Oct 2025 18:39:52 +0530 Subject: [PATCH 1/3] Create ProductOfArrayExceptSelf.py --- .../arrays/ProductOfArrayExceptSelf.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 data_structures/arrays/ProductOfArrayExceptSelf.py diff --git a/data_structures/arrays/ProductOfArrayExceptSelf.py b/data_structures/arrays/ProductOfArrayExceptSelf.py new file mode 100644 index 000000000000..02e5b1369795 --- /dev/null +++ b/data_structures/arrays/ProductOfArrayExceptSelf.py @@ -0,0 +1,76 @@ +from typing import List +import doctest + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + """ + 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. + + This solution runs in O(n) time and O(1) extra space (excluding output), + without using division. + + >>> Solution().productExceptSelf([1, 2, 3, 4]) + [24, 12, 8, 6] + >>> Solution().productExceptSelf([-1, 1, 0, -3, 3]) + [0, 0, 9, 0, 0] + >>> Solution().productExceptSelf([0, 0, 0]) + [0, 0, 0] + >>> Solution().productExceptSelf([0, 1, 2, 3]) + [6, 0, 0, 0] + >>> Solution().productExceptSelf([0, 0, 1]) + [0, 0, 0] + >>> Solution().productExceptSelf([-1, -2, -3]) + [6, 3, 2] + >>> Solution().productExceptSelf([1, 2]) + [2, 1] + >>> Solution().productExceptSelf([1, 1, 1]) + [1, 1, 1] + >>> Solution().productExceptSelf([-30, 30, -30, 30]) + [-27000, 27000, -27000, 27000] + >>> Solution().productExceptSelf([5, 0, 0, 5]) + [0, 0, 0, 0] + """ + n = len(nums) + answer = [1] * n + + # Left pass: Compute prefix products + left_product = 1 + for i in range(n): + answer[i] *= left_product + left_product *= nums[i] + + # Right pass: Compute suffix products and multiply into answer + right_product = 1 + for i in range(n - 1, -1, -1): + answer[i] *= right_product + right_product *= nums[i] + + return answer + +if __name__ == "__main__": + # Run doctests + doctest.testmod() + + # Additional manual execution example + sol = Solution() + test_cases = [ + [1, 2, 3, 4], + [-1, 1, 0, -3, 3], + [0, 0, 0], + [0, 1, 2, 3], + [0, 0, 1], + [-1, -2, -3], + [1, 2], + [1, 1, 1], + [-30, 30, -30, 30], + [5, 0, 0, 5] + ] + + print("\nRunning additional test cases:") + for case in test_cases: + result = sol.productExceptSelf(case) + print(f"Input: {case} -> Output: {result}") + From aa6ff0dd3a07f303112834dea736e980aca5cb17 Mon Sep 17 00:00:00 2001 From: Saurabh Kokate Date: Sat, 4 Oct 2025 18:41:58 +0530 Subject: [PATCH 2/3] Update ProductOfArrayExceptSelf.py --- .../arrays/ProductOfArrayExceptSelf.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/data_structures/arrays/ProductOfArrayExceptSelf.py b/data_structures/arrays/ProductOfArrayExceptSelf.py index 02e5b1369795..d1fa3c5f47c3 100644 --- a/data_structures/arrays/ProductOfArrayExceptSelf.py +++ b/data_structures/arrays/ProductOfArrayExceptSelf.py @@ -50,6 +50,24 @@ def productExceptSelf(self, nums: List[int]) -> List[int]: return answer +# #optimized Answer +# class Solution(object): +# def productExceptSelf(self, nums): +# m = 1 +# z = 0 +# for i in nums: +# if i !=0: +# m*= i +# else: +# z+=1 + +# if z==1: +# return [m if i==0 else 0 for i in nums] +# if z>1: +# return [0]*len(nums) +# return [m//i for i in nums] + + if __name__ == "__main__": # Run doctests doctest.testmod() From 92128342b1fd3e02d4a1313bff3a5864a03fab04 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 4 Oct 2025 13:27:33 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/ProductOfArrayExceptSelf.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/ProductOfArrayExceptSelf.py b/data_structures/arrays/ProductOfArrayExceptSelf.py index d1fa3c5f47c3..60f11b334fbb 100644 --- a/data_structures/arrays/ProductOfArrayExceptSelf.py +++ b/data_structures/arrays/ProductOfArrayExceptSelf.py @@ -1,6 +1,7 @@ from typing import List import doctest + class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: """ @@ -50,6 +51,7 @@ def productExceptSelf(self, nums: List[int]) -> List[int]: return answer + # #optimized Answer # class Solution(object): # def productExceptSelf(self, nums): @@ -84,11 +86,10 @@ def productExceptSelf(self, nums: List[int]) -> List[int]: [1, 2], [1, 1, 1], [-30, 30, -30, 30], - [5, 0, 0, 5] + [5, 0, 0, 5], ] print("\nRunning additional test cases:") for case in test_cases: result = sol.productExceptSelf(case) print(f"Input: {case} -> Output: {result}") -