diff --git a/javascript/1822-sign-of-the-product-of-an-array.js b/javascript/1822-sign-of-the-product-of-an-array.js new file mode 100644 index 000000000..95607d3bd --- /dev/null +++ b/javascript/1822-sign-of-the-product-of-an-array.js @@ -0,0 +1,10 @@ +const arraySign = function (nums) { + let sign = 1; + + for (const num of nums) { + if (num == 0) return 0; + if (num < 0) sign = -1 * sign; + } + + return sign; +}; diff --git a/python/1822-sign-of-the-product-of-an-array.py b/python/1822-sign-of-the-product-of-an-array.py new file mode 100644 index 000000000..4ec193faa --- /dev/null +++ b/python/1822-sign-of-the-product-of-an-array.py @@ -0,0 +1,14 @@ +from typing import List + + +class Solution: + def arraySign(self, nums: List[int]) -> int: + sign = 1 + + for num in nums: + if num == 0: + return 0 + if num < 0: + sign = -1 * sign + + return sign diff --git a/typescript/1822-sign-of-the-product-of-an-array.ts b/typescript/1822-sign-of-the-product-of-an-array.ts new file mode 100644 index 000000000..3ed7fd6cb --- /dev/null +++ b/typescript/1822-sign-of-the-product-of-an-array.ts @@ -0,0 +1,10 @@ +function arraySign(nums: number[]): number { + let sign = 1; + + for (const num of nums) { + if (num == 0) return 0; + if (num < 0) sign = -1 * sign; + } + + return sign; +}