Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

How to power-hungry foobar solution in java (Java Programming Language)

3 Answer(s) Available
Answer # 1 #

The challenge is to find the maximum product of a subset of a given array. Here's the problem:

My code basically removes all 0's inside the list, then I iterate over all the remaining integers in the array and put each one in their respective array (positive or negative numbers). I sort my array with negative numbers then check its length; if it's odd, then I remove the last element (which will always be the one closest to 0 since I sorted it). Finally, I multiply each number inside these two arrays and return the result (we need to return it as string).

I've already search the internet as to why my logic is failing the fourth test case, I've read that if there's only one negative number in the array, you should return 0, but I also read that you should return the negative value. However, previously to writing the part of my code that checks this possibility, the fifth test case was also failing. So you need to return 0 in this case.

Here's the repl with my code, open test cases and some test cases that I've added.

[5]
Edit
Query
Report
Valsa, Valsan Jani
Agricultural Consultant
Answer # 2 #

{2,3,2,2} -> 24

{-2,-2,-3} -> 6

{-2,-2,-2,-3} -> 24

{-2,-2,0,0,-2,-3} -> 24

{2,3,0,0,2,2} -> 24

{-2,-2,0,0,2,3} -> 24

{0,0} -> 0

and also in foobar challenge the size of the result to return will 10^50 might be possible.

So it is also recommended to implement a product using String.

import java.util.Arrays;

public class Main {

[5]
Edit
Query
Report
Hemky MacMurray
Space Nursing
Answer # 3 #
1
package com.google.challenges;
2
import java.math.BigInteger;
3
public class Answer {
4
public static String answer (int[] xs){
5
    BigInteger result = new BigInteger(1);
6
    int xsLen = xs.length, pos = 0,ng=0;
7
    int[] negatives = new int[xsLen];
8
    if (xsLen == 1){
9
        return Integer.toString(xs[0]);
10
    }
11
    for (int n = 0;n < xsLen;n++){
12
        int val = xs[n];
13
        if (val == 0){
14
            continue;
15
        }
16
        if (val > 0){
17
            result = result.multiply(new BigInteger(Integer.toString(val)));
18
            ng++;
19
        } else {
20
            negatives[pos] = val;
21
            pos++;
22
        }
23
    }
24
    if(ng==0)
25
    {
26
        int l=0;
27
        return Integer.toString(l);
28
    }
29
    if ((pos % 2) == 0){
30
        for (int i = 0;i < pos;i++){
31
            result = result.multiply(new BigInteger(Integer.toString(negatives[i])));
32
        }
33
    } else {
34
        int min = -1000; int mPos = -1;
35
        for (int i = 0;i < pos;i++){
36
            if(negatives[i] > min){
37
                min = negatives[i];
38
                mPos = i;
39
            }
40
        }
41
        for (int j = 0;j < pos;j++){
42
            if(j == mPos){
43
                continue;
44
            }
45
            result = result.multiply(new BigInteger(Integer.toString(negatives[j])));
46
        }
47
    }
48
    return result.toString();
49
}
50
}
[1]
Edit
Query
Report
Halloun kijog Deepu
CUTTING MACHINE OPERATOR