Hello, I need help with one of my questions on Google foobar, this is what I still have.
package com.google.challenges;
import java.math.BigInteger;
public class Answer{
public static String answer (int[] xs){
BigInteger result = new BigInteger("1");
int xsLen = xs.length, pos = 0;
int[] negatives = new int[xsLen];
if (xsLen == 1){
return Integer.toString(xs[0]);
}
for (int n = 0;n < xsLen;n++){
int val = xs[n];
if (val == 0){
continue;
}
if (val > 0){
result = result.multiply(new BigInteger(Integer.toString(val)));
} else {
negatives[pos] = val;
pos++;
}
}
if ((pos % 2) == 0){
for (int i = 0;i < pos;i++){
result = result.multiply(new BigInteger(Integer.toString(negatives[i])));
}
} else {
int min = -1000; int mPos = -1;
for (int i = 0;i < pos;i++){
if(negatives[i] > min){
min = negatives[i];
mPos = i;
}
}
for (int j = 0;j < pos;j++){
if(j == mPos){
continue;
}
result = result.multiply(new BigInteger(Integer.toString(negatives[j])));
}
}
return result.toString();
}
}
here is the question
, , , , , . (xs), , , . , [2, -3, 1, 0, -5], , : xs [0] = 2, xs [1 ] = -3, xs [4] = -5, 2 * (- 3) * (- 5) = 30. ([2, -3,1,0, -5]) "30".
1 50 , , 1000 ( , , , ). , .
Python, solution.py
Java, solution.java
Inputs:
(int list) xs = [2, 0, 2, 2, 0]
Output:
(string) "8"
Inputs:
(int list) xs = [-2, -3, 4, -5]
Output:
(string) "60"
2 , , , ! , .:)