Skip to main content

Sign of the Product of an Array - Solution & Explanation

EasyArrayMath14 min readAsked at: Amazon, Microsoft, Meta
Practice this problem

Problem Statement

Implement 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).

 

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 Overview

Problem Overview: You receive an integer array nums and must return the sign of the product of all elements. The result is 1 if the product is positive, -1 if negative, and 0 if any element makes the product zero. Computing the full product can overflow quickly, so the task is really about reasoning about signs rather than multiplication.

Approach 1: Zero and Negative Count Check (O(n) time, O(1) space)

Instead of multiplying every element, iterate through the array once and track two conditions: whether a zero exists and how many negative numbers appear. If any element equals 0, the product is immediately 0. Otherwise, the sign depends entirely on the parity of the negative count. An even number of negatives produces a positive product, while an odd number produces a negative product. This works because sign multiplication follows simple parity rules and avoids overflow entirely. The solution only requires a single pass through the array and constant extra memory.

Approach 2: Multiplicative Identity and Sign Evaluation (O(n) time, O(1) space)

Another method keeps a running sign variable initialized to 1, which represents the multiplicative identity. Traverse the array and update the sign as you go. If you encounter 0, return 0 immediately since the product becomes zero. For each negative number, flip the current sign by multiplying by -1. Positive numbers leave the sign unchanged. This approach mimics multiplication behavior but only tracks sign transitions instead of actual values. It is a clean implementation grounded in basic math properties and still requires just a single linear scan.

Recommended for interviews: The zero and negative count method is typically what interviewers expect. It clearly demonstrates your understanding that only the count of negative values and the presence of zero determine the final sign. Mentioning the overflow issue with direct multiplication shows practical engineering awareness. The running-sign variant is equally optimal and slightly more elegant, but both approaches demonstrate the same O(n) reasoning over the array.

Approach 1: Zero and Negative Count Check

This approach involves scanning through the array once to check for any zeros and counting the number of negative numbers. If there's a zero, the product is zero. Otherwise, if the number of negative numbers is odd, the product is negative; if even, the product is positive.

The C solution uses a loop to iterate over the array. If a zero is found immediately return zero, otherwise count the number of negative integers. If the count is even, return 1. If odd, return -1.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of elements in the array. We only need to pass over the array once.
Space Complexity: O(1), as no additional space beyond the given variables is used.

Try this approach in the editor →

Approach 2: Multiplicative Identity and Sign Evaluation

This approach uses a multiplicative identity set as variable 'sign', initialized to 1. Then iterates through each element of the array, multiplying 'sign' with -1 for each negative number and returning zero if a zero is found.

This C code sets a 'sign' variable to 1 and iterates over the input. It flips the sign by multiplying it by -1 for each negative number found, returning 0 immediately upon encountering a zero.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Direct Traversal

The problem requires us to return the sign of the product of the array elements, i.e., return 1 for positive numbers, -1 for negative numbers, and 0 if it equals 0.

We can define an answer variable ans, initially set to 1.

Then we traverse each element v in the array. If v is a negative number, we multiply ans by -1. If v is 0, we return 0 in advance.

After the traversal is over, we return ans.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1).

Code

Python

Java

C++

Go

Rust

JavaScript

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Zero and Negative Count Check

Time Complexity: O(n), where n is the number of elements in the array. We only need to pass over the array once.
Space Complexity: O(1), as no additional space beyond the given variables is used.

Multiplicative Identity and Sign Evaluation

Time Complexity: O(n)
Space Complexity: O(1)

Direct Traversal—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Zero and Negative Count CheckO(n)O(1)Best general solution; clearly explains the role of zero detection and negative parity.
Multiplicative Identity and Sign EvaluationO(n)O(1)Useful when modeling sign behavior directly without counting negatives.

Video Solution

Sign of An Array - Leetcode 1822 - Python • NeetCodeIO • 8,136 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Sign of the Product of an Array easy or hard?
Sign of the Product of an Array is classified as an easy problem. The challenge is recognizing that you do not need to compute the actual product. Instead, tracking zeros and the parity of negative numbers leads to a straightforward O(n) solution.
Sign of the Product of an Array Python/Java solution
In Python or Java, iterate through the array and maintain a counter for negative numbers or a running sign variable. Return 0 if a zero is encountered. After the loop, determine whether the final sign is positive or negative based on the negative count parity. The logic remains identical across languages.
How to solve Sign of the Product of an Array in O(n)?
Iterate through the array and track two conditions: whether a zero exists and how many negative values appear. Return 0 immediately if a zero is found. If no zero exists, return 1 when the number of negatives is even and -1 when it is odd. This avoids computing the full product and prevents overflow.
What is the best approach for Sign of the Product of an Array?
The best approach scans the array once and counts negative numbers while checking for zero. If any element is zero, the result is 0. Otherwise, the product is positive when the negative count is even and negative when it is odd. This method runs in O(n) time and uses O(1) extra space.
Is Sign of the Product of an Array asked at Google/Amazon/Meta?
Variants of sign evaluation and product reasoning appear in interviews at companies like Amazon, Google, and Meta. While this exact problem is categorized as easy, it tests fundamental array traversal and mathematical reasoning, which frequently appear in screening rounds.
What data structure is used in Sign of the Product of an Array?
The solution primarily uses a simple array traversal. No additional data structures such as hash maps or stacks are required. Only constant variables are needed to track zero presence or the parity of negative numbers.
What is the time complexity of Sign of the Product of an Array?
The optimal solution runs in O(n) time because every element in the array must be inspected at least once. Space complexity is O(1) since only a few counters or a sign variable are maintained regardless of input size.

Ready to solve this problem?

Practice Sign of the Product of an Array with our built-in code editor and test cases.

Practice on FleetCode