Skip to main content
Back to problems
#1822
Easy Algorithms

Sign of the product of an array

Array Math
64.8% acceptance
Feb 25, 2026
2290
231
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).

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn array_sign(nums: Vec<i32>) -> i32 {
    let mut neg = 0i32;
    for &n in &nums {
      if n == 0 { return 0; }
      if n < 0 { neg += 1; }
    }
    if neg % 2 == 1 { -1 } else { 1 }
  }
}