Product of Array Except Self
Part of Practice: Beyond the Basics
Given an integer array nums, return a new array answer where answer[i] is the product of every element of nums except nums[i] itself. Two rules make it interesting: you may not use division, and the whole thing must run in O(n) time.
The array has at least 2 and up to 100,000 elements, values range from -30 to 30, and zeros are allowed. All intermediate products are guaranteed to fit in a 32-bit integer.
Examples
- Input:
- nums = [1, 2, 3, 4]
- Output:
- [24, 12, 8, 6]
answer[0] = 2·3·4 = 24, answer[1] = 1·3·4 = 12, and so on.
- Input:
- nums = [-1, 1, 0, -3, 3]
- Output:
- [0, 0, 9, 0, 0]
Every position except index 2 includes the 0 in its product. answer[2] = -1·1·-3·3 = 9.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.