Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1822. Sign of the Product of an Array - Leetcode Solution

Code Implementation

class Solution:
    def arraySign(self, nums):
        sign = 1
        for num in nums:
            if num == 0:
                return 0
            elif num < 0:
                sign *= -1
        return sign
      
class Solution {
public:
    int arraySign(vector<int>& nums) {
        int sign = 1;
        for (int num : nums) {
            if (num == 0) return 0;
            else if (num < 0) sign *= -1;
        }
        return sign;
    }
};
      
class Solution {
    public int arraySign(int[] nums) {
        int sign = 1;
        for (int num : nums) {
            if (num == 0) return 0;
            else if (num < 0) sign *= -1;
        }
        return sign;
    }
}
      
var arraySign = function(nums) {
    let sign = 1;
    for (let num of nums) {
        if (num === 0) return 0;
        else if (num < 0) sign *= -1;
    }
    return sign;
};
      

Problem Description

You are given an array of integers, nums. Your task is to determine the sign of the product of all elements in the array, without actually calculating the product itself. The sign of a number is defined as:

  • 1 if the number is positive
  • -1 if the number is negative
  • 0 if the number is zero
Return 1 if the product of all the elements is positive, -1 if negative, and 0 if the product is zero.

Constraints:

  • The array can contain positive, negative, and zero values.
  • You should not actually compute the product, as it may cause integer overflow.

Thought Process

At first glance, it may seem like we need to multiply all the numbers in nums and then check if the result is positive, negative, or zero. However, multiplying large numbers may cause overflow, and it's unnecessary to compute the actual product just to know its sign.

The key observation is that:

  • If any number in the array is zero, the entire product becomes zero.
  • Every negative number flips the sign of the product.
  • The actual magnitude of the numbers doesn't matter—only their sign matters for this problem.
So, instead of multiplying the numbers, we can:
  • Check for zeros as we iterate.
  • Count how many negative numbers there are.
  • The product is positive if there are an even number of negatives, and negative if there are an odd number of negatives.

Solution Approach

Let's break down the solution step-by-step:

  1. Initialize a sign variable: Start with sign = 1. This will keep track of the sign of the product as we process each number.
  2. Iterate through each element in the array:
    • If the current number is 0, immediately return 0 since the product will be zero.
    • If the current number is negative, flip the sign by multiplying sign by -1.
    • If the number is positive, do nothing (the sign remains the same).
  3. Return the sign: After going through the whole array, return the sign variable. It will be 1 if the product is positive, -1 if negative.

This method is efficient because:

  • We never actually multiply the numbers, so there's no risk of overflow.
  • We only do a single pass through the array.
  • We use a simple integer variable to track the sign, which is memory efficient.

Example Walkthrough

Let's use the example nums = [-1, 2, -3, 4]:

  1. Start with sign = 1.
  2. First number is -1 (negative): flip sign to -1.
  3. Second number is 2 (positive): sign stays -1.
  4. Third number is -3 (negative): flip sign to 1.
  5. Fourth number is 4 (positive): sign stays 1.

At the end, the sign is 1, meaning the product is positive. If there had been a zero anywhere, we would have returned 0 immediately.

Time and Space Complexity

Brute-force approach:

  • If we actually multiplied all numbers, time complexity would be O(n) for n elements, but with a risk of integer overflow.
  • Space complexity is O(1) since we only store the product.
Optimized approach (our solution):
  • Time complexity: O(n) — we make one pass through the array.
  • Space complexity: O(1) — only a single variable is used to track the sign.

The optimized method is both safe and efficient, avoiding the risk of overflow.

Summary

By focusing on the sign rather than the actual product, we can efficiently determine the answer in a single pass without risk of overflow. The key insight is that only the number of negative numbers and the presence of zero matter. This approach is simple, elegant, and robust, making it ideal for both small and large input arrays.