Expressions Matter

May 07, 2018

Question:

 

Given three integers a ,b ,creturn the largest number obtained after inserting the following operators and brackets+*().

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.

 

Solution:

function expressionMatter(a, b, c) {
    var r = Math.max(
        a+b+c,
        a*b*c,
        a*b+c,
        a*(b+c),
        a+b*c,
        (a+b)*c
    );
    return r// highest achievable result
}

This question is referred from Codewars