#K36407. Product Except Self

    ID: 25747 Type: Default 1000ms 256MiB

Product Except Self

Product Except Self

Given an array of integers \(nums\), construct an array \(result\) such that \(result[i]\) is equal to the product of all the elements of \(nums\) except \(nums[i]\). You are not allowed to use division in your solution.

For example, if \(nums = [1, 2, 3, 4]\), then \(result = [24, 12, 8, 6]\) because:

  • \(result[0] = 2 \times 3 \times 4 = 24\)
  • \(result[1] = 1 \times 3 \times 4 = 12\)
  • \(result[2] = 1 \times 2 \times 4 = 8\)
  • \(result[3] = 1 \times 2 \times 3 = 6\)

The solution should run in \(O(n)\) time and use \(O(1)\) additional space (excluding the output array).

inputFormat

The first line contains a single integer \(n\) denoting the number of elements in the array. The second line contains \(n\) space-separated integers representing the array \(nums\).

outputFormat

Output one line containing \(n\) space-separated integers, where the \(i\)-th integer is the product of all the elements of \(nums\) except \(nums[i]\).

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