#K79492. Product of Non-Zero Elements
Product of Non-Zero Elements
Product of Non-Zero Elements
You are given a list of n integers. Your task is to compute the product of all non-zero integers in the list. If the list contains only zeroes, output 0
.
Mathematically, if we denote the list as \(a_1, a_2, \dots, a_n\), then you need to compute:
\(\prod_{i=1}^{n} a_i'\)
where \(a_i' = a_i\) if \(a_i \neq 0\) and is ignored otherwise. If all \(a_i = 0\), the answer is defined to be 0
.
The input list is guaranteed to contain at least one integer.
inputFormat
The first line contains an integer n
— the number of elements in the list. The second line contains n
space-separated integers.
outputFormat
Output a single integer — the product of all non-zero integers in the list. If the list contains only zeroes, output 0
.
4
1 2 3 4
24
</p>