The problem. Given an integer array nums, return an array answer where answer[i] is the product of every element of nums except nums[i]. It must run in O(n) time and you may not use division.
Input: nums = [1, 2, 3, 4]
Output: [24, 12, 8, 6]
Input: nums = [-1, 1, 0, -3, 3]
Output: [0, 0, 9, 0, 0]For each position you need the product of all the other numbers. If division were allowed, you could multiply everything once and divide by nums[i] — but that breaks as soon as the array contains a 0, which is exactly why the problem forbids it.
Free account
Sign up to read the rest of this lesson: 5 more sections, 3 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come