# Rod cutting question using recursion

hey there.I am a beginner and i am learning recursion these days.
I came across with this problem.And i am having some queries.

#include <bits stdc++.h>
using namespace std;

int rodCutting(int n, int a, int b, int c)
{
if(n == 0)
return 0;
if(n < 0)
return -1;
int res = max(max(rodCutting(n-a, a, b, c), rodCutting(n-b, a, b, c)),
rodCutting(n-c, a, b, c));

if(res== -1)
return -1;
return 1+res;
}

int main()
{
int n = 25;
int a = 11, b = 12, c = 13;
cout << rodCutting(n, a, b, c);
}

please explain me how return res+1 is working.I mean we are initializing it again and again how it is incrementing its value??? Please help!!!

As you are calling function recursively each function will have its own res variable and the res which you are returning gets initialized to the local res of that function.

1 Like

Can you explain it with an example???or Any link or article by which i can understand it better. @khan_rahim

can you explain how by debugging this code for-
n=5 a=2 b=1 c=5
we are getting 5 as output by this code.