Sign of the Product of an Array

signFunc(x) returns 1 when x is positive, -1 when x is negative and 0 when x is 0. Given an integer array nums, let product be the product of all its values, and return signFunc(product).

30:00

Sign of the Product of an Array
easy
Free written solution
Topics
Companies

signFunc(x) returns 1 when x is positive, -1 when x is negative and 0 when x is 0. Given an integer array nums, let product be the product of all its values, and return signFunc(product).

Example 1:
Input: {"nums":[-1,-2,-3,-4,3,2,1]}
Output: 1
Constraints:
  • 1≤nums.length≤10001 \leq \text{nums.length} \leq 1000

  • −100≤nums[i]≤100-100 \leq \text{nums}[i] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[-1,-2,-3,-4,3,2,1]}

The real product of 7 numbers can be enormous, so track only its sign.

nums (green positive, red negative)

-1
-2
-3
-4
3
2
1

running sign

+

negatives cancel in pairs

none yet

negatives = 0 (even)

Variables
VariableValue
n7
DepthFunction Call
Stack empty
0/16