Skip to content

1822 - Sign of the Product of an Array #2688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions javascript/1822-sign-of-the-product-of-an-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const arraySign = function (nums) {
let sign = 1;

for (const num of nums) {
if (num == 0) return 0;
else if (num < 0) sign = -1 * sign;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Avoid else

if (canSkip) continue;
if (canBreak) break;
if (canExit) return;

// else ...

if (isPositive) sign *= -1;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code!

}

return sign;
};
14 changes: 14 additions & 0 deletions python/1822-sign-of-the-product-of-an-array.py
Original file line number Diff line number Diff line change
@@ -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
elif num < 0:
sign = -1 * sign

return sign
10 changes: 10 additions & 0 deletions typescript/1822-sign-of-the-product-of-an-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function arraySign(nums: number[]): number {
let sign = 1;

for (const num of nums) {
if (num == 0) return 0;
else if (num < 0) sign = -1 * sign;
}

return sign;
}