Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

power-hungry foobar solution in java

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();
}
}
Comment

PREVIOUS NEXT
Code Example
Java :: priority queue java comparator 
Java :: remove all element in a list java 
Java :: what is static setter and getter examples in java 
Java :: java read file from command line argument 
Java :: Programme to calculate Combination 
Java :: java home 
Java :: draw circle with fill java 
Java :: Java LogManager 
Java :: how to create hashmap 
Java :: android get string value 
Java :: how to declare jtextfield 
Java :: get role assigned to a user inside spring controller 
Java :: java break and continue 
Java :: practical1_answer 
Java :: crit chance in java 
Java :: java generics 
Java :: java grösser gleich 
Java :: android text change java debounce 
Java :: quebra de linha java 
Java :: Delete Specials Caractères from a String in java 
Java :: how to get start char in string in java 
Java :: Iterating Through the Java Map 
Java :: how to develop web apps using java 
Java :: intellij set target bytecode version 
Java :: Add an element to ArrayList using addall() method 
Java :: split in java 
Java :: initialize an arraylist in java in java 
Java :: creating jdbc connection in java using service name oracle 
Java :: java list of lists 
Java :: mongodb spring boot index usage 0 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =