Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5005aab

Browse files
committed
Have implemented the program to find the sign of the product of any array.
1 parent d72e701 commit 5005aab

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

.idea/workspace.xml

Lines changed: 25 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Sign of the Product of an Array
3+
Link: https://leetcode.com/problems/sign-of-the-product-of-an-array/
4+
*/
5+
package com.raj;
6+
7+
public class SignOfTheProductOfAnArray {
8+
public static void main(String[] args) {
9+
// Initialization.
10+
int[] nums = {-1, -2, -3, -4, 3, 2, 1};
11+
int counter = 0;
12+
int ans = 1;
13+
14+
// Logic.
15+
for (int i : nums) {
16+
if (i == 0) {
17+
ans = 0;
18+
break;
19+
}
20+
if (i < 0) {
21+
counter++;
22+
}
23+
}
24+
if (ans != 0 && counter % 2 != 0) {
25+
ans = -1;
26+
}
27+
28+
// Display the result.
29+
System.out.println("The sign of the product of any array is: " + ans);
30+
}
31+
}

0 commit comments

Comments
 (0)