call(back)
Algorithms & Data StructureseasySign in to star problems

Array Products II

Turn an array into one where each position holds the product of the next two elements, wrapping around the end: productOfNextTwo(nums).

productOfNextTwo([3, 4, 5])      // [20, 15, 12]
productOfNextTwo([1, 2, 3, 4])   // [6, 12, 4, 2]

Inputs are integers, may include zeros and negatives, and have at least three elements.

Hints

Hint 1

The two neighbors of index i are (i + 1) % n and (i + 2) % n — one map does it.

Asked at