forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request 7oSkaaa#1026 from NouraAlgohary/main
add day 2 modified
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
... Sign of the Product of an Array/02- Sign of the Product of an Array (Noura Algohary).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Author: Noura Algohary | ||
class Solution { | ||
public: | ||
int arraySign(vector<int>& nums) { | ||
int signCounter = 0; | ||
|
||
for(int num : nums) | ||
{ | ||
// if one zero appears, the result is zero | ||
if(num==0) | ||
return 0; | ||
else if(num<0) | ||
signCounter++; | ||
} | ||
|
||
// if negative numbers are of odd count, the result is negative | ||
if (signCounter%2==0) | ||
return 1; | ||
else | ||
return -1; | ||
} | ||
}; |
14 changes: 14 additions & 0 deletions
14
...- Sign of the Product of an Array/02- Sign of the Product of an Array (Noura Algohary).py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Author: Noura Algohary | ||
class Solution: | ||
def arraySign(self, nums: List[int]) -> int: | ||
signCounter = 0 | ||
|
||
for num in nums: | ||
# if one zero appears, the result is zero | ||
if num == 0: | ||
return 0 | ||
elif num < 0: | ||
signCounter += 1 | ||
|
||
# if negative numbers are of odd count, the result is negative | ||
return 1 if signCounter % 2 == 0 else -1 |