I'm having a tough time in understanding the following code based on recursion algorithm in Java. I don't understand, what's the different value x
and y
have when they call within one another? I tried to get the right value by calling System.out.print()
within a code, but still get no help.
public class RecursionExample
{
private static int[][] arr={
{3},
{7, 4},
{2, 4, 6},
{8 ,5, 9, 3}
};
public static int maxSum(int[][] graph, int x, int y, int sum) {
if (x == 3)
{
return sum+graph[x][y];
}
int max= Math.max(maxSum(graph, x+1, y, sum), maxSum(graph, x+1, y+1, sum));
sum += graph[x][y];
return sum+max;
}
public static void main(String[] ar)
{
System.out.println(maxSum(arr,0,0,0));
}
}
I'm not a master in programming, and I'm trying to learn Java by doing. Any help is appreciated.