#K63712. Product Array Except Self

    ID: 31815 Type: Default 1000ms 256MiB

Product Array Except Self

Product Array Except Self

Given an array \( arr[0 \ldots n-1] \) of \( n \) integers, your task is to construct an output array \( output[0 \ldots n-1] \) such that each \( output[i] \) is equal to the product of all the elements of the input array except \( arr[i] \). In other words, for every valid index \( i \), \[ output[i] = \prod_{\substack{0 \leq j

Constraints:

  • Time complexity must be \( O(n) \) (linear time).
  • No division operation is allowed.

Example:

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

Explanation: For index 0, product = 234 = 24, and similarly for others.

inputFormat

The first line of the input contains a single integer \( n \) (the size of the array). The second line contains \( n \) space-separated integers representing the array elements.

outputFormat

Output a single line with \( n \) space-separated integers where the \( i^{th} \) integer represents the product of all numbers in the array except the number at index \( i \).

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