title: Rod cutting - recursive
body: <p>Question: <a href="http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/" rel="nofollow noreferrer">http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/</a></p>
  
  <p>My code at: <a href="http://ideone.com/HGXk3t" rel="nofollow noreferrer">http://ideone.com/HGXk3t</a></p>
  
  <p>I tried solving for recursive problem but getting unexpected result.
  For rod cutting problem, <strong>I am taking max of two calls of the same recursive function: One using that index and another without using price at that index.</strong>
  But the answer is not what it should be.
  Thanks in advance.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  // A utility function to get the maximum of two integers
  int max(int a, int b) { return (a &gt; b)? a : b;}
  
  /* Returns the best obtainable price for a rod of length n and
     price[] as prices of different pieces */
  int cutRod(int price[], int n, int i)
  {
      if(n&lt;1 || i==0)
          return 0;
  
      return max(price[i-1] + cutRod(price,n-(i+1),i), cutRod(price,n,i-1));
  }
  
  /* Driver program to test above functions */
  int main()
  {
      int arr[] = {17, 17, 1};
      int size = sizeof(arr)/sizeof(arr[0]);
      printf("Maximum Obtainable Value is %d\n", cutRod(arr, size, size));
      getchar();
      return 0;
  }
  </code></pre>