You must write an algorithm that runs in O(n) time and without using the division operator.
Example 1:
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Explanation:
For index 0 ā 2 Ć 3 Ć 4 = 24
For index 1 ā 1 Ć 3 Ć 4 = 12
For index 2 ā 1 Ć 2 Ć 4 = 8
For index 3 ā 1 Ć 2 Ć 3 = 6
Example 2:
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Explanation:
For index 2, the product becomes:
(-1) Ć 1 Ć (-3) Ć 3 = 9
All other positions include the element 0, so their product becomes 0.
Example 3:
Input: nums = [2,3,4,5]
Output: [60,40,30,24]
Explanation:
For each index, we multiply all remaining elements except the current element itself.