The challenge
Task
- Given three integers
a
,b
,c
, return the largest number obtained after inserting the following operators and brackets:+
,*
,()
- In other words , try every combination of a,b,c with [*+()] , and return the Maximum Obtained
Consider an Example :
With the numbers are 1, 2 and 3 , here are some ways of placing signs and brackets:
1 * (2 + 3) = 5
1 * 2 * 3 = 6
1 + 2 * 3 = 7
(1 + 2) * 3 = 9
So the maximum value that you can obtain is 9.
Notes
- The numbers are always positive.
- The numbers are in the range (1 ≤ a, b, c ≤ 10).
- You can use the same operation more than once.
- It’s not necessary to place all the signs and brackets.
- Repetition in numbers may occur .
- You cannot swap the operands. For instance, in the given example you cannot get expression
(1 + 3) * 2 = 8
.
Input » Output Examples:
expressionsMatter(1,2,3) ==> return 9
Explanation:
After placing signs and brackets, the Maximum value obtained from the expression (1+2) * 3 = 9
.
expressionsMatter(1,1,1) ==> return 3
Explanation:
After placing signs, the Maximum value obtained from the expression is 1 + 1 + 1 = 3
.
expressionsMatter(9,1,1) ==> return 18
Explanation:
After placing signs and brackets, the Maximum value obtained from the expression is 9 * (1+1) = 18
.
Test cases
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class ExpressionsMatter {
@Test
public void checkSmallValues() {
assertEquals(6, Biggest.expressionsMatter(2, 1, 2));
assertEquals(3, Biggest.expressionsMatter(1, 1, 1));
assertEquals(4, Biggest.expressionsMatter(2, 1, 1));
assertEquals(9, Biggest.expressionsMatter(1, 2, 3));
assertEquals(5, Biggest.expressionsMatter(1, 3, 1));
assertEquals(8, Biggest.expressionsMatter(2, 2, 2));
}
@Test
public void checkIntermediateValues() {
assertEquals( 20, Biggest.expressionsMatter(5, 1, 3));
assertEquals(105, Biggest.expressionsMatter(3, 5, 7));
assertEquals( 35, Biggest.expressionsMatter(5, 6, 1));
assertEquals( 8, Biggest.expressionsMatter(1, 6, 1));
assertEquals( 14, Biggest.expressionsMatter(2, 6, 1));
assertEquals( 48, Biggest.expressionsMatter(6, 7, 1));
}
@Test
public void checkMixedValues() {
assertEquals( 60, Biggest.expressionsMatter( 2, 10, 3));
assertEquals( 27, Biggest.expressionsMatter( 1, 8, 3));
assertEquals(126, Biggest.expressionsMatter( 9, 7, 2));
assertEquals( 20, Biggest.expressionsMatter( 1, 1, 10));
assertEquals( 18, Biggest.expressionsMatter( 9, 1, 1));
assertEquals(300, Biggest.expressionsMatter(10, 5, 6));
assertEquals( 12, Biggest.expressionsMatter( 1, 10, 1));
}
}
The solution in Java
Option 1:
public class Biggest {
public static int expressionsMatter(int a, int b, int c) {
int out = a+b+c;
if ((a * (b + c))>out) out = a * (b + c);
if ((a * b * c)>out) out = a * b * c;
if ((a + b * c)>out) out = a + b * c;
if ((a + b) * c>out) out = (a + b) * c;
return out;
}
}
Option 2 (using Math.max
):
public class Biggest {
public static int expressionsMatter(int a, int b, int c) {
return Math.max(Math.max(a + b + c, a * b * c),Math.max ((a + b) * c, a * (b + c)));
}
}
Option 3 (using Vector
):
import java.util.*;
public class Biggest {
public static int expressionsMatter(int a, int b, int c) {
Vector <Integer> sol = new Vector();
sol.add(a + b + c);
sol.add(a * b * c);
sol.add((a+b)*c);
sol.add(a*(c+b));
Collections.sort(sol);
return sol.lastElement();
}
}
Option 4 (using a stream
):
import java.util.stream.IntStream;
public class Biggest {
public static int expressionsMatter(int a, int b, int c) {
return IntStream.of(a + b + c, a * b * c, a + b * c, a * b + c, (a + b) * c, a * (b + c)).max().getAsInt();
}
}