title: RodCutting: when the rod is bigger than the array length
body: <p>How can I modify the methods cutRod and bottomUpCutRod to hold lengths that are bigger than the array length. For example, currently p has length 11, how can I cut the rod of length 15, 20 , etc, having this same array. For example</p>
  
  <p><code>p = {0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30};</code></p>
  
  <p>if I call cutRod(p,10), I get 30, but it crashes of course in cutRod(p,15) or
  cutRod(p,20). (same applies for bottomUpCutRod). Any ideas how to do this?. This is dynamic programming problem, my idea to implement bottomUpCutRod method is to traverse p and for each element calculate every permutation of itself and its neighbors and update if necessary the resulting array, r. </p>
  
  <pre><code> public class Main {    
      private static final double MINUS_INFINITY = Double.NEGATIVE_INFINITY;    
  
      public static void main(String[] args) {
  
          // price array
          double[] p = {0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30};
  
          // test cutRod
          cutRod(p,10);
  
          // test bottomUpCutRod
         bottomUpCutRod(p,10);
  
      }//end of main
  
      // an optimal cut for a rod of length n
      // p is the price array
      // use recursion
      private static double cutRod(double[] p, int value) {
          double[] r = new double[value+1];
          double out = 0;
  
            // initialize r to NEGATIVE_INFINITY;
          for (int i = 1; i &lt; r.length; i++)
              r[i] = MINUS_INFINITY;
  
          // call the helper method
          out = helpCutRod(p,r.length-1,r);
  
          // print r
          System.out.println("Result ");
          System.out.println("r[" + (r.length-1) + "] = " + r[r.length-1]);
  
          return out;
      }//end of method             
  
      // helpCutRod computes an optimal cut for a rod 
      // p is the price array and r[i] is the optimal cut for a rod of length i
      // n is the length of the rod that is currently being computed
  
      private static double helpCutRod(double[] p, int n, double[] r) {
          double q = MINUS_INFINITY;
          if (r[n] &gt;= 0)  // the whole r was computed
              return r[n];
          if (n == 0)
             q = 0;
          else {
              for (int i = 1; i &lt;= n; i++) {                
                   q = RodCutting.max(q, p[i] + helpCutRod(p,n-i,r));
              }    
              r[n] = q;
          }           
          return q;
      }                
  
      // use the bottom-up approach
      // do NOT use recursion
  
      private static double bottomUpCutRod(double[] p, int len) {
          // r[i] is the optimal cut for a rod of length i
          double[] r = new double[len+1];
          r[0] = 0;
          for (int j = 1; j &lt; p.length; j++) {
  
              // compute r[j]
              double q = MINUS_INFINITY;
  
              // r[j] is the maximum over i of p[i] + r[j-i]
              // where 1&lt;= i &lt;= j
  
              for (int i = 1; i &lt;= j; i++)                
                  q = max(q, p[i] + r[j-i]);
  
              // update value of r[j]
              r[j] = q;
          }//end of for outer    
  
          // print r 
          System.out.println("The r array from the bottomUpCutRod:");
              System.out.println("r[" + len + "] = " + r[len]);
          return r[len] ;       
      }//end of method
      public static double max(double a, double b){
  
          if(a&lt;=b){
              return b;
          }else{
              return a;
          }   
      }//end of max
  }//end of class
  </code></pre>