Google Foobar power_hungry

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]);
        }
        // Split the input up into pos/negative. Pos get put onto the final value, as they don't need anything else.
        // they are all useful. negative to onto seperate array and get sorted later
        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++;
            }
        }
        // even number of negatives means a full product will always be positive.
        // odd number means that we discard the smallest number to maximise the result.
        if ((pos % 2) == 0){
            // even number, so add to result
            for (int i = 0;i < pos;i++){
                result = result.multiply(new BigInteger(Integer.toString(negatives[i])));
            }
        } else {
            // sort then discard the minimum
            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])));
            }
        }

        // done, return the string;
        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 , , , ! , .:)

+4
2

:

1 50 Integer -1000 1000. , : [0, 0, -43, 0].

    if (xsLen == 1){
        return Integer.toString(xs[0]);
    }

. ( ). 0 .

, . BigInteger , .

, , , Integer BigInteger. , " ". "Greatest Negative Integer", .

... , .

+2

, , , , 0. .

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,ng=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)));
            ng++;
        } else {
            negatives[pos] = val;
            pos++;
        }
    }
    if(ng==0)
    {
        int l=0;
        return Integer.toString(l);
    }
    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();
}
}
0

Source: https://habr.com/ru/post/1658082/


All Articles