238. Product of Array Except Self
Approach 1: Calculate products from both sides
In a single for loop keep multiplying terms from left side and right side respectively and multiply those two to get the required product
Time Complexity: O(N)
Space Complexity: O(1)
Approach 2: Calculate products from both sides in 2 arrays
In a single loop build 2 arrays that contain the product of all the numbers to the left of a position, and all the numbers to the right of a position. Once those arrays are constructed, you can simply iterate through these index by index and multiply the left product at the previous index, and the right product at the next index to build the array of required products
Time Complexity: O(N)
Space Complexity: O(N)
Illegal Approach: Division
Time Complexity: O(N)
Space Complexity: O(1)
Last updated