Talentd
FleetCode
Make and follow sheets.Sign in

Sign of the Product of an Array solution and explanation

EasyArrayMath4 min read64.8% acceptanceUpdated 9 Apr 2026
Sign of An Array - Leetcode 1822 - PythonNeetCodeIO · 8:30 · 8K viewsWatch on YouTube
9 more videos on this problem
  1. Sign of An Array - Leetcode 1822 - PythonNeetCodeIO · 8:30 · 8K views
  2. 1822. Sign of the Product of an Array | Leetcode Weekly Contest 236Ayushi Rawat · 4:40 · 2K views
  3. Leetcode Solution 1822 - Sign of the Product of an ArrayJohn Leonardo · 2:47 · 1K views
  4. Leetcode | 1822. Sign of the Product of an Array | Easy | Java SolutionDeveloper Docs · 6:04 · 1K views
  5. LEETCODE - 1822 || Sign of the Product of an Array || Easy solutionhi-malik · 4:57 · 593 views
  6. 1822. Sign of the Product of an Array (Leetcode Easy)Programming Live with Larry · 2:27 · 576 views
  7. Sign of the Product of an Array - LeetCode 1822 Solution in C++ (LeetCode Math)nexTRIE · 2:54 · 206 views
  8. LeetCode #1822 Sign of the Product of an Array Coding Interview Problem with Java SolutionThe Orange Sauce · 2:30 · 196 views
  9. Sign of the Product of an Array - LeetCode #1822 - Python, JavaScript, Java, C++CodingNinja · 5:44 · 187 views
  10. 1822. Sign of the Product of an Array | Leetcode | Number Theory | FundamentalsCoding with Tamilarasan · 5:11 · 184 views

The problem

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.

def arraySign(nums):
    negative_count = 0
    for num in nums:
        if num == 0:
            return 0
        if num < 0:
            negative_count += 1
    return 1 if negative_count % 2 == 0 else -1

print(arraySign([-1, -2, -3, -4, 3, 2, 1]))
public class Main {
    public static int arraySign(int[] nums) {
        int negativeCount = 0;
        for (int num : nums) {
            if (num == 0) return 0;
            if (num < 0) negativeCount++;
        }
        return (negativeCount % 2 == 0) ? 1 : -1;
    }

    public static void main(String[] args) {
        int[] nums = {-1,-2,-3,-4,3,2,1};
        System.out.println(arraySign(nums));
    }
}
#include <vector>
using namespace std;

int arraySign(vector<int>& nums) {
    int negative_count = 0;
    for (int num : nums) {
        if (num == 0) return 0;
        if (num < 0) negative_count++;
    }
    return (negative_count % 2 == 0) ? 1 : -1;
}

int main() {
    vector<int> nums = {-1, -2, -3, -4, 3, 2, 1};
    printf("%d\n", arraySign(nums));
    return 0;
}
function arraySign(nums) {
    let negativeCount = 0;
    for (let num of nums) {
        if (num === 0) return 0;
        if (num < 0) negativeCount++;
    }
    return negativeCount % 2 === 0 ? 1 : -1;
}

console.log(arraySign([-1, -2, -3, -4, 3, 2, 1]));
#include <stdio.h>

int arraySign(int* nums, int numsSize) {
    int negative_count = 0;
    for (int i = 0; i < numsSize; i++) {
        if (nums[i] == 0) return 0;
        if (nums[i] < 0) negative_count++;
    }
    return (negative_count % 2 == 0) ? 1 : -1;
}

int main() {
    int nums[] = {-1, -2, -3, -4, 3, 2, 1};
    int size = sizeof(nums) / sizeof(nums[0]);
    printf("%d\n", arraySign(nums, size));
    return 0;
}
using System;

public class Solution {
    public static int ArraySign(int[] nums) {
        int negativeCount = 0;
        foreach (int num in nums) {
            if (num == 0) return 0;
            if (num < 0) negativeCount++;
        }
        return (negativeCount % 2 == 0) ? 1 : -1;
    }

    public static void Main() {
        int[] nums = {-1, -2, -3, -4, 3, 2, 1};
        Console.WriteLine(ArraySign(nums));
    }
}

The Python solution maintains a similar process to other languages with Pythonic constructs. The list is iterated, and the return depends on zero presence and negative count parity.

Complexity

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

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.

def arraySign(nums):
    sign = 1
    for num in nums:
        if num == 0:
            return 0
        if num < 0:
            sign *= -1
    return sign

print(arraySign([-1, -2, -3, -4, 3, 2, 1]))
public class Main {
    public static int arraySign(int[] nums) {
        int sign = 1;
        for (int num : nums) {
            if (num == 0) return 0;
            if (num < 0) sign *= -1;
        }
        return sign;
    }

    public static void main(String[] args) {
        int[] nums = {-1,-2,-3,-4,3,2,1};
        System.out.println(arraySign(nums));
    }
}
#include <vector>
using namespace std;

int arraySign(vector<int>& nums) {
    int sign = 1;
    for (int num : nums) {
        if (num == 0) return 0;
        if (num < 0) sign *= -1;
    }
    return sign;
}

int main() {
    vector<int> nums = {-1, -2, -3, -4, 3, 2, 1};
    printf("%d\n", arraySign(nums));
    return 0;
}
function arraySign(nums) {
    let sign = 1;
    for (let num of nums) {
        if (num === 0) return 0;
        if (num < 0) sign *= -1;
    }
    return sign;
}

console.log(arraySign([-1, -2, -3, -4, 3, 2, 1]));
#include <stdio.h>

int arraySign(int* nums, int numsSize) {
    int sign = 1;
    for (int i = 0; i < numsSize; i++) {
        if (nums[i] == 0) return 0;
        if (nums[i] < 0) sign *= -1;
    }
    return sign;
}

int main() {
    int nums[] = {-1, -2, -3, -4, 3, 2, 1};
    int size = sizeof(nums) / sizeof(nums[0]);
    printf("%d\n", arraySign(nums, size));
    return 0;
}
using System;

public class Solution {
    public static int ArraySign(int[] nums) {
        int sign = 1;
        foreach (int num in nums) {
            if (num == 0) return 0;
            if (num < 0) sign *= -1;
        }
        return sign;
    }

    public static void Main() {
        int[] nums = {-1, -2, -3, -4, 3, 2, 1};
        Console.WriteLine(ArraySign(nums));
    }
}

Python uses a similar pattern, where an initialized 'sign' is multiplied by -1 for every negative, zero results in an immediate 0 return.

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

func arraySign(nums []int) int {
	ans := 1
	for _, v := range nums {
		if v == 0 {
			return 0
		}
		if v < 0 {
			ans *= -1
		}
	}
	return ans
}
impl Solution {
    pub fn array_sign(nums: Vec<i32>) -> i32 {
        let mut ans = 1;
        for &num in nums.iter() {
            if num == 0 {
                return 0;
            }
            if num < 0 {
                ans *= -1;
            }
        }
        ans
    }
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Zero and Negative Count Check

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

Multiplicative Identity and Sign Evaluation

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

Direct Traversal–

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.

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.

Comments

Now write it yourselfClose this page and solve Sign of the Product of an Array in the editor, without looking.
Practise Sign of the Product of an Array