#C7723. Product of Array Except Self

    ID: 51626 Type: Default 1000ms 256MiB

Product of Array Except Self

Product of Array Except Self

Given an array \( nums \) of \( n \) integers, compute an output array such that each element at index \( i \) of the output is equal to the product of all the elements of \( nums \) except \( nums[i] \). You must solve this problem without using division.

Example:

Input: 4
       1 2 3 4
Output: 24 12 8 6

The products are computed as follows:

  • For index 0: \( 2 \times 3 \times 4 = 24 \)
  • For index 1: \( 1 \times 3 \times 4 = 12 \)
  • For index 2: \( 1 \times 2 \times 4 = 8 \)
  • For index 3: \( 1 \times 2 \times 3 = 6 \)

Note: The input format and the output format are described below.

inputFormat

The input is given via stdin and has the following format:

n
num1 num2 ... num_n

Where the first line contains a single integer \( n \) (the number of elements in the array) and the second line contains \( n \) space-separated integers.

outputFormat

Output the resulting array via stdout in a single line where the elements are separated by a space.

## sample
4
1 2 3 4
24 12 8 6

</p>