LeetCode Entry

1822. Sign of the Product of an Array

2.05.2023 easy 2023 kotlin

There is an sign function in kotlin, but leetcode.com doesn't support it yet.

1822. Sign of the Product of an Array easy


fun arraySign(nums: IntArray): Int = nums.fold(1) { r, t -> if (t == 0) 0 else r * (t / Math.abs(t)) }

blog post substack

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/199

Intuition

Do what is asked, but avoid overflow.

Approach

There is an sign function in kotlin, but leetcode.com doesn’t support it yet. We can use fold.

Complexity

  • Time complexity: \(O(n)\)
  • Space complexity: \(O(1)\)