title: Get minimum and maximum value of expression by using brackets
body: <p>I have one-line expression without brackets, for instance <code>1+5*6-3</code>. We can use all operators (<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>). What I need to know? What is maximum and minimum value of expression when we can use brackets.  </p>
  
  <p><strong>Examples</strong> </p>
  
  <p><em>1)</em><br>
  Input: <code>1+5*6-3</code><br>
  Output: <code>33 16</code> </p>
  
  <p><em>Explanation</em>: <code>33 = (((1+5)*6)-3)</code> | <code>16 = (1+(5*(6-3))</code></p>
  
  <p><em>2)</em><br>
  Input: <code>15*5-12-9</code><br>
  Output: <code>72 -240</code> </p>
  
  <p><em>3)</em><br>
  Input: <code>3*10*16-14-11*2</code><br>
  Output: <code>954 -600</code> </p>
  
  <p><em>4)</em><br>
  Input: <code>8-5*18+5-13+0*2+14-6*6+1</code><br>
  Output: <code>7233 -13482</code> </p>
  
  <p><em>5)</em><br>
  Input: <code>6+5-15*18+14-3-5-3-2-2*8-14+12</code><br>
  Output: <code>11081 -4935</code> </p>
  
  <p><em>6)</em><br>
  Input: <code>13-4*6*18+12+8-5*8-4+2+11*6+9-7+6*12*18+8-7+3*1-15*1-12*1-12-14+7-14*9*6</code><br>
  Output: <code>74388962089190 -72949270802716</code> </p>
  
  <p>And why I'm writing this post? I have fast algorithm, but it works for tests number <code>1</code>, <code>2</code>, <code>3</code>, but for <code>4</code>, <code>5</code>, <code>6</code> a get bad results. Why? Maybe you will see what I have done wrong. Here is my code:</p>
  
  <pre><code>public static long[] parenthesis(String expression, int start, int end) {
      if(resultStorage[start][end][2] == 1) 
          return resultStorage[start][end];
  
      if(isNumber(expression)) 
          return new long[] { Long.parseLong(expression), Long.parseLong(expression), 1 };
  
      long[] result = { Integer.MAX_VALUE, Integer.MIN_VALUE, 1 };
      for(int i = 1; i &lt; expression.length() - 1; i++) {
          if(Character.isDigit(expression.charAt(i))) 
              continue;
  
          long[] left = parenthesis(expression.substring(0, i), start, start + i);
          long[] right = parenthesis(expression.substring(i + 1, expression.length()), start + i + 1, end);
          switch(expression.charAt(i)) {
              case '+' : result[0] = Math.min(result[0], left[0] + right[0]);
                         result[1] = Math.max(result[1], left[1] + right[1]); 
                         break;
              case '-' : result[0] = Math.min(result[0], left[0] - right[0]);
                         result[1] = Math.max(result[1], left[1] - right[1]); 
                         break;
              case '*' : result[0] = Math.min(result[0], left[0] * right[0]);
                         result[1] = Math.max(result[1], left[1] * right[1]); 
                         break;
              case '/' : if(right[0] != 0) 
                             result[0] = Math.min(result[0], left[0] / right[0]);
                         if(right[1] != 0)
                             result[1] = Math.max(result[1], left[1] / right[1]); 
                         break;
          }
      }
  
      resultStorage[start][end] = result;
      return result;
  }
  </code></pre>