-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution3.java
More file actions
93 lines (82 loc) · 2.81 KB
/
Copy pathSolution3.java
File metadata and controls
93 lines (82 loc) · 2.81 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.math.BigInteger;
public class Solution3 {
public static String solution(int[] xs) {
BigInteger[] val = new BigInteger[xs.length];
int[] counter = new int[] {0,0,0};
BigInteger[] answer = {BigInteger.ONE,BigInteger.ZERO,BigInteger.ONE};
BigInteger minNegative = BigInteger.ONE;
for(int i=0; i<xs.length; i++)
{
val[i] = new BigInteger(Long.toString(xs[i]));
if(val[i].compareTo(BigInteger.ZERO)<0)
{
counter[0]++;
answer[0] = answer[0].multiply(val[i]);
if(minNegative.compareTo(BigInteger.ONE)==0)
{
minNegative = val[i];
}
else if(val[i].compareTo(minNegative)>0)
{
minNegative = val[i];
}
}
else if(val[i].compareTo(BigInteger.ZERO)==0)
{
counter[1]++;
}
else if(val[i].compareTo(BigInteger.ZERO)>0)
{
counter[2]++;
answer[2] = answer[2].multiply(val[i]);
}
}
//System.out.println(minNegative);
BigInteger retBigInteger = BigInteger.ZERO;
String ret="";
if(counter[2]>0)
{
//System.out.println("There is at least 1 positive integer");
if(answer[0].compareTo(BigInteger.ZERO)>0)
{
retBigInteger = answer[0].multiply(answer[2]);
}
else
{
retBigInteger = answer[0].multiply(answer[2]).divide(minNegative);
}
ret = retBigInteger.toString();
}
else if(counter[0]>0) // no positive integer
{
//System.out.println("No positive integer in testcase");
if(counter[0]==1 && counter[1]==0)
{
retBigInteger = answer[0];
}
else if(counter[0]==1 && counter[1]>0)
{
retBigInteger = BigInteger.ZERO;
}
else
{
retBigInteger = answer[0];
if(answer[0].compareTo(BigInteger.ZERO)<0)
{
retBigInteger = answer[0].divide(minNegative);
}
}
ret = retBigInteger.toString();
}
else //all zero
{
//System.out.println("All zero");
ret="0";
}
return ret;
}
public static void main(String[] args)
{
System.out.println(Solution3.solution(new int[] {5,6}));
}
}