Given an integer array nums, return a new array result of the same length where each element at index i is replaced by the product of the elements at indices i+1 and i+2 in the original array.
If either i+1 or i+2 is out of bounds, set result[i] to 0.
Function Signature
def product_of_next_two(nums: list[int]) -> list[int]:
Rules
-
Every product uses values from the original
nums
, never values already written to
result
.
-
When
nums
has at least two elements, the last two entries of
result
are always
0
. When it has one element,
result
is
[0]
.
Constraints
-
1 <= nums.length <= 10^4
-
-10^3 <= nums[i] <= 10^3
Examples
Input: nums = [1,2,3,4,5]
Output: [6,12,20,0,0]
result[0] = 2 * 3 = 6, result[1] = 3 * 4 = 12 and result[2] = 4 * 5 = 20. Indices 3 and 4 have fewer than two elements after them, so they are 0.
Input: nums = [2,3,4]
Output: [12,0,0]
Only index 0 has two elements after it, and 3 * 4 = 12.
Input: nums = [4,-1,-6,2]
Output: [6,-12,0,0]
result[0] = (-1) * (-6) = 6 and result[1] = (-6) * 2 = -12. The last two entries are 0.