1
+ '''
2
+ 238. Product of Array Except Self
3
+
4
+ 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].
5
+
6
+ The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
7
+
8
+ You must write an algorithm that runs in O(n) time and without using the division operation.
9
+
10
+
11
+
12
+ Example 1:
13
+
14
+ Input: nums = [1,2,3,4]
15
+ Output: [24,12,8,6]
16
+ Example 2:
17
+
18
+ Input: nums = [-1,1,0,-3,3]
19
+ Output: [0,0,9,0,0]
20
+
21
+
22
+ Constraints:
23
+
24
+ 2 <= nums.length <= 105
25
+ -30 <= nums[i] <= 30
26
+ The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer.
27
+
28
+
29
+ Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
30
+ '''
31
+
32
+ # Brute Force (Inbuilt math function approach)
33
+ # Time Complexity: O(n^2)
34
+ # Space Complexity: O(n)
35
+
36
+ import math
37
+
38
+ class Solution :
39
+ def productExceptSelf (self , nums ):
40
+ result = []
41
+
42
+ for i in range (len (nums )):
43
+ new_arr = nums [:i ] + nums [i + 1 :]
44
+ res = math .prod (new_arr )
45
+ result .append (res )
46
+ return result
47
+
48
+
49
+ # Brute Force (Without using inbuilt math function)
50
+ # Time Complexity: O(n^2)
51
+ # Space Complexity: O(n)
52
+
53
+ '''
54
+ In this approach, we will iterate through the array and for each element, we will calculate the product of all elements except the current element.
55
+ We will store the result in the result array and return it.
56
+ '''
57
+
58
+ class Solution :
59
+ def productExceptSelf (self , nums ):
60
+ result = []
61
+
62
+ for i in range (len (nums )):
63
+ prod = 1
64
+ for j in range (len (nums )):
65
+ if i != j :
66
+ prod *= nums [j ]
67
+ result .append (prod )
68
+
69
+ return result
70
+
71
+ # Optimized Approach
72
+ # Time Complexity: O(n)
73
+ # Space Complexity: O(n)
74
+
75
+ '''
76
+ In this approach, we will first calculate the prefix product of the array by iterating from right to left and store it in an array pref[].
77
+ Then we will calculate the suffix product of the array by iterating through left to right and store it in an array suff[].
78
+
79
+ Finally, we will calculate the product of pref[i]*suff[i] for each element of the array and store it in the result array.
80
+ '''
81
+ class Solution :
82
+ def productExceptSelf (self , nums ):
83
+ n = len (nums )
84
+ prod = [1 ]* n
85
+ pref = [1 ]* n
86
+ suff = [1 ]* n
87
+
88
+ prefix = 1
89
+
90
+ for i in range (n ):
91
+ pref [i ]= prefix
92
+ prefix *= nums [i ]
93
+
94
+ suffix = 1
95
+
96
+ for i in range (n - 1 ,- 1 ,- 1 ):
97
+ suff [i ]= suffix
98
+ suffix *= nums [i ]
99
+
100
+ for i in range (n ):
101
+ prod [i ]= pref [i ]* suff [i ]
102
+
103
+ return prod
104
+
105
+ # Optimized Approach
106
+ # Time Complexity: O(n)
107
+ # Space Complexity: O(1)
108
+
109
+ '''
110
+ This approach calculates the product of all elements except nums[i] using two passes:
111
+
112
+ Prefix Pass (left to right) stores the product of all elements before nums[i].
113
+
114
+ Suffix Pass (right to left) multiplies it with the product of all elements after nums[i].
115
+ This achieves O(n) time and O(1) extra space by updating the result in-place.
116
+ '''
117
+
118
+ class Solution :
119
+ def productExceptSelf (self , nums ):
120
+ n = len (nums )
121
+ prod = [1 ]* n
122
+
123
+ prefix = 1
124
+
125
+ for i in range (n ):
126
+ prod [i ]= prefix
127
+ prefix *= nums [i ]
128
+
129
+ suffix = 1
130
+
131
+ for i in range (n - 1 ,- 1 ,- 1 ):
132
+ prod [i ]*= suffix
133
+ suffix *= nums [i ]
134
+
135
+ return prod
136
+
137
+
138
+
139
+
140
+ obj = Solution ()
141
+ nums = [1 ,2 ,4 ,6 ]
142
+ print (obj .productExceptSelf (nums ))
0 commit comments