Back to DSA sheet

Sign of An Array

EasyArrays

Let product be the multiplication of all array elements. Return 1 if product is positive, -1 if negative, and 0 if it is zero. You should not actually compute the product (it can overflow).

Input format (stdin): the first line has n. The second line has n integers. Output 1, -1, or 0.

Examples
Input: 4 -1 -2 -3 4
Output: -1
An odd number of negatives makes the product negative.
Input: 3 1 5 0
Output: 0
A zero makes the product zero.
Constraints
  • 1 <= n <= 1000
  • -100 <= nums[i] <= 100
Sheets
NeetCode 250
sign-of-an-array.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.