Back to blog
Mar 15, 2026
4 min read

Sign of the Product of an Array

Determine the sign of the product of an array of integers without calculating the actual product to avoid overflow.

Difficulty: Easy | Acceptance: 64.80% | Paid: No Topics: Array, Math

There is a function signFunc(x) that returns:

  • 1 if x is positive.
  • -1 if x is negative.
  • 0 if x is equal to 0.

You are given an integer array nums. Let product be the product of all values in the array nums.

Return signFunc(product).

Examples

Example 1:

Input: nums = [-1,-2,-3,-4,3,2,1]
Output: 1
Explanation: The product of all values in the array is 144, and signFunc(144) = 1.

Example 2:

Input: nums = [1,5,0,2,-3]
Output: 0
Explanation: The product of all values in the array is 0, and signFunc(0) = 0.

Example 3:

Input: nums = [-1,1,-1,1,-1]
Output: -1
Explanation: The product of all values in the array is -1, and signFunc(-1) = -1.

Constraints

1 <= nums.length <= 1000
-100 <= nums[i] <= 100

Approach 1: Direct Multiplication

Intuition The most straightforward way to solve this problem is to calculate the product of all numbers in the array and then check its sign. This directly follows the problem statement.

Steps

  • Initialize a variable product to 1.
  • Iterate through the array nums, multiplying each element into product.
  • After the loop, check if product is greater than 0 (return 1), less than 0 (return -1), or equal to 0 (return 0).
python
class Solution:
    def arraySign(self, nums: list[int]) -&gt; int:
        product = 1
        for num in nums:
            product *= num
        if product &gt; 0:
            return 1
        elif product &lt; 0:
            return -1
        else:
            return 0

Complexity

  • Time: O(n) - We iterate through the array once.
  • Space: O(1) - We only use a single variable to store the product (ignoring the space for the BigInt object itself in languages like Java/JS).
  • Notes: In languages with fixed-size integers (C++, Java with int), this approach risks overflow because the product can grow up to 100¹⁰⁰⁰, which exceeds the limit of a 64-bit integer (2⁶³-1). To make it runnable in Java and JavaScript, we use arbitrary-precision integers (BigInteger and BigInt). In C++, long long will overflow for large inputs, making this approach technically incorrect for the upper constraint limits without a big-integer library.

Approach 2: Counting Negatives

Intuition We can determine the sign of a product without calculating the product itself. The sign is positive if the number of negative integers is even, and negative if the count is odd. If any number is zero, the product is immediately zero.

Steps

  • Initialize a counter negCount to 0.
  • Iterate through the array nums:
    • If the current number is 0, return 0 immediately.
    • If the current number is negative, increment negCount.
  • After the loop, if negCount is even, return 1. Otherwise, return -1.
python
class Solution:
    def arraySign(self, nums: list[int]) -&gt; int:
        neg_count = 0
        for num in nums:
            if num == 0:
                return 0
            if num &lt; 0:
                neg_count += 1
        return -1 if neg_count % 2 else 1

Complexity

  • Time: O(n) - We traverse the array once.
  • Space: O(1) - We only use a single integer counter.
  • Notes: This is the optimal approach as it avoids any potential overflow issues and uses minimal memory.