</>CodexBash
Back to problems
Not started

Product of everything else

Medium
2000 ms limit256 MB memoryPrefix sumsArrays

Stuck?

Show a hint

For each position you need the product of everything to its left and everything to its right. Two passes, one from each end.

Next in this lesson

Running sum

For each position, give the product of every number in the list except the one at that position. Do it without using division.

Worked example#

For [1, 2, 3, 4]:

positioneverything elseproduct
02, 3, 424
11, 3, 412
21, 2, 48
31, 2, 36

So the answer is [24, 12, 8, 6].

Dividing the total product by each number would be quick, but it breaks the moment the list contains a zero. Instead, note that everything else is everything to the left times everything to the right. Two passes, one in each direction, gives both.

Constraints#

  • 2 <= n <= 100000
  • The answer always fits in a 64 bit integer

Input

nums is a count then that many integers.

Output

The products, separated by spaces.

Examples

Input

4
1 2 3 4

Output

24 12 8 6

At position 0 the rest are 2, 3 and 4, which multiply to 24.

Input

2
2 3

Output

3 2

Each answer is simply the other number.

Input

3
1 0 3

Output

0 3 0

A zero makes every other answer zero, and the zero's own answer is the rest multiplied.

Something wrong with this problem? Report an issue

Ready to try it

Write your solution in C, C++, Java or Python and see every test case come back with the time and memory it took.

Free, and it stays free. No card.