Skip to content

Commit d26ba3a

Browse files
committed
New Problem Solution - "1822. Sign of the Product of an Array"
1 parent 1629a81 commit d26ba3a

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1822|[Sign of the Product of an Array](https://leetcode.com/problems/sign-of-the-product-of-an-array/) | [C++](./algorithms/cpp/signOfTheProductOfAnArray/SignOfTheProductOfAnArray.cpp)|Easy|
1213
|1819|[Number of Different Subsequences GCDs](https://leetcode.com/problems/number-of-different-subsequences-gcds/) | [C++](./algorithms/cpp/numberOfDifferentSubsequencesGcds/NumberOfDifferentSubsequencesGcds.cpp)|Hard|
1314
|1818|[Minimum Absolute Sum Difference](https://leetcode.com/problems/minimum-absolute-sum-difference/) | [C++](./algorithms/cpp/minimumAbsoluteSumDifference/MinimumAbsoluteSumDifference.cpp)|Medium|
1415
|1817|[Finding the Users Active Minutes](https://leetcode.com/problems/finding-the-users-active-minutes/) | [C++](./algorithms/cpp/findingTheUsersActiveMinutes/FindingTheUsersActiveMinutes.cpp)|Medium|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Source : https://leetcode.com/problems/sign-of-the-product-of-an-array/
2+
// Author : Hao Chen
3+
// Date : 2021-04-11
4+
5+
/*****************************************************************************************************
6+
*
7+
* There is a function signFunc(x) that returns:
8+
*
9+
* 1 if x is positive.
10+
* -1 if x is negative.
11+
* 0 if x is equal to 0.
12+
*
13+
* You are given an integer array nums. Let product be the product of all values in the array nums.
14+
*
15+
* Return signFunc(product).
16+
*
17+
* Example 1:
18+
*
19+
* Input: nums = [-1,-2,-3,-4,3,2,1]
20+
* Output: 1
21+
* Explanation: The product of all values in the array is 144, and signFunc(144) = 1
22+
*
23+
* Example 2:
24+
*
25+
* Input: nums = [1,5,0,2,-3]
26+
* Output: 0
27+
* Explanation: The product of all values in the array is 0, and signFunc(0) = 0
28+
*
29+
* Example 3:
30+
*
31+
* Input: nums = [-1,1,-1,1,-1]
32+
* Output: -1
33+
* Explanation: The product of all values in the array is -1, and signFunc(-1) = -1
34+
*
35+
* Constraints:
36+
*
37+
* 1 <= nums.length <= 1000
38+
* -100 <= nums[i] <= 100
39+
******************************************************************************************************/
40+
41+
class Solution {
42+
public:
43+
int arraySign(vector<int>& nums) {
44+
int negtive=0;
45+
for(auto& n : nums) {
46+
if (n==0) return 0;
47+
if (n < 0) negtive++;
48+
}
49+
return negtive % 2 == 0 ? 1 : -1;
50+
}
51+
};

0 commit comments

Comments
 (0)