# Calculate the biggest of 3 numbers using Java ## The challenge

• 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.

• 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:

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
`expressionsMatter(1,2,3)  ==>  return 9`Code language: PHP (php)```

### Explanation:

After placing signs and brackets, the Maximum value obtained from the expression `(1+2) * 3 = 9`.

``expressionsMatter(1,1,1)  ==>  return 3`Code language: PHP (php)`

### Explanation:

After placing signs, the Maximum value obtained from the expression is `1 + 1 + 1 = 3`.

``expressionsMatter(9,1,1)  ==>  return 18`Code language: PHP (php)`

### 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));
}
}
```Code language: Java (java)```

## 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;
}
}
```Code language: Java (java)```

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)));
}
}
```Code language: Java (java)```

Option 3 (using `Vector`):

See also  How to Sum Consecutives in Java
``````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);

Collections.sort(sol);

return sol.lastElement();
}
}
```Code language: Java (java)```

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();
}
}
```Code language: Java (java)```

0