Sign of Product of Array - aloalgo

Sign of Product of Array

Easy

You are given an integer array nums.

Your task is to determine the sign of the product of all elements in the array.

Note that:

  • If the product is negative, return -1.
  • If the product is zero, return 0.
  • If the product is positive, return 1.

Return -1, 0, or 1 representing the sign of the product.

Example 1

Input
[-1, -2, -3, -4, 3, 2, 1]
Output
1
Explanation:

The product of all values in the array is 144, which is positive, so we return 1.

Example 2

Input
[1, 5, 0, 2, -3]
Output
0
Explanation:

The array contains 0, so the product is 0, and we return 0.

Example 3

Input
[-1, 1, -1, 1, -1]
Output
-1
Explanation:

The product of all values in the array is -1, which is negative, so we return -1.

Loading...
Input
[-1, -2, -3, -4, 3, 2, 1]
Output
1

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!