Product of Array Except Self
Try to solve the Product of Array Except Self problem.
We'll cover the following
Statement#
You’re given an integer array, arr. Return a resultant array so that res[i] is equal to the product of all the elements of arr except arr[i].
Write an algorithm that runs in time without using the division operation.
Constraints:
-
arr.length -
arr[i] - The product of any prefix or suffix of
arris guaranteed to fit in a 32-bit integer.
Examples#
1 of 2
2 of 2
Understand the problem#
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Product of Array Except Self
What is the output if the following array is given as input?
arr = [-1, 2, 3, 5, 0]
[30, -15, -10, -6, -30]
[0, -15, -10, -6, -30]
[30, -15, -10, -6, 0]
[0, 0, 0, 0, -30]
Try it yourself#
Implement your solution in the following coding playground:
The optimal solution to this problem runs in O(n) time and takes O(1) space.
You might want to go over the Two Pointers pattern again.
Solution: Container With the Most Water
Solution: Product of Array Except Self