Product of Array Except Self
MediumGiven an integer array nums
, return an array answer
such that answer[i]
is equal to the product of all elements in nums
except nums[i]
. The solution must run in O(N) time and should not use division.
Example:
Input: nums = [1,2,3,4] Output: [24,12,8,6] Explanation: - answer[0] = 2 × 3 × 4 = 24 - answer[1] = 1 × 3 × 4 = 12 - answer[2] = 1 × 2 × 4 = 8 - answer[3] = 1 × 2 × 3 = 6
Test Cases
Test Cases
Input
1 2 3 4
Expected Output
24 12 8 6
Step 1
Step 2
Step 3
Step 4
Step 1: Identify the Pattern
Product of Array Except Self
MediumGiven an integer array nums
, return an array answer
such that answer[i]
is equal to the product of all elements in nums
except nums[i]
. The solution must run in O(N) time and should not use division.
Example:
Input: nums = [1,2,3,4] Output: [24,12,8,6] Explanation: - answer[0] = 2 × 3 × 4 = 24 - answer[1] = 1 × 3 × 4 = 12 - answer[2] = 1 × 2 × 4 = 8 - answer[3] = 1 × 2 × 3 = 6
Test Cases
Test Cases
Input
1 2 3 4
Expected Output
24 12 8 6
Step 1
Step 2
Step 3
Step 4
Step 1: Identify the Pattern