Skip to content

Commit 6a1b483

Browse files
Merge pull request #3104 from nkawaller/sign-prod-array-go
Create: 1822-sign-of-the-product-of-an-array.go
2 parents ad0f0a4 + 2ee7fe1 commit 6a1b483

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Time: O(n)
3+
Space: O(1)
4+
*/
5+
6+
func arraySign(nums []int) int {
7+
neg := 0
8+
for _, n := range nums {
9+
if n == 0 {
10+
return 0
11+
} else if n < 0 {
12+
neg++
13+
}
14+
}
15+
if neg%2 == 0 {
16+
return 1
17+
} else {
18+
return -1
19+
}
20+
}

0 commit comments

Comments
 (0)