Page MenuHomedesp's stash

Solution.java
No OneTemporary

Solution.java

import java.util.Arrays;
import java.math.BigInteger;
public class Solution {
public static String solution(int[] xs) {
//assume xs never drops below 1 element as stated in the constraint
int negCount = 0, remove = -1;
Arrays.sort(xs);
for(int i = 0; i < xs.length; i++) {
int x = xs[i];
if(x < 0) {
negCount++;
remove = i; //get last negative value's index to be removed
}
}
if(xs.length > 1 && remove != -1 && negCount % 2 != 0) xs[remove] = 0; //if a remove value is found and it's not the only value, and negative count is not even (multiplying will return negative)
BigInteger result = BigInteger.ZERO; //initialize big integer since it can get pretty big according to the readme
for(int x : xs) {
if(x != 0)
result = result == BigInteger.ZERO ? BigInteger.valueOf(x) : result.multiply(BigInteger.valueOf(x)); //only multiply non zero values; zero values are almost always discardable
}
return result.max(BigInteger.valueOf(xs[xs.length - 1])).toString(); //check if result is smaller than largest value in array (usually for catching 0s that we discarded)
}
}

File Metadata

Mime Type
text/x-java
Expires
Mon, May 11, 5:50 PM (1 d, 18 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
7f/68/5abf5c60024504915cb3e6f4370b

Event Timeline