-
Notifications
You must be signed in to change notification settings - Fork 0
/
1822.c
40 lines (34 loc) · 804 Bytes
/
1822.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
int arraySign(int* nums, int numsSize);
int main()
{
int n;
scanf("%d", &n);
int nums[1001];
for(int i = 0; i < n; ++i) scanf("%d", &nums[i]);
int ans = arraySign(nums, n);
printf("%d\n", ans);
return 0;
}
int arraySign(int* nums, int numsSize){
int pos = 1;
for(int i = 0; i < numsSize; ++i)
{
if(nums[i] == 0) return 0;
if(nums[i] < 0) pos *= -1;
}
return pos;
}
/*
Best Solution
int arraySign(int* nums, int numsSize)
{
int countNegatives = 0;
for (int i = 0; i < numsSize; i++)
{
if (nums[i] == 0) return 0; // Product is 0
if (nums[i] < 0) countNegatives++; // Increment count of negatives
}
return (countNegatives % 2) == 0 ? 1 : -1; // True: Positive, False: Negative
}
*/