Perform a box trace. Show trace
int[] arr = {8, 10, 4, -2, 2, 8, 6, 4, 3, 9};
System.out.println(fun(arr, 7, 1));
int fun(int nums[], int aa, int bb) {
int ret;
System.out.println(aa);
if(aa <= 1)
ret = nums[bb];
else
ret = nums[bb-1] + fun(nums, aa - 2, bb +1);
System.out.println(ret);
return ret;
}
arr[] = {8, 10, 4, -2, 2, 8, 6, 4, 3, 9}
fun(arr, 7, 1) returns arr[0] + fun(arr, 5, 2)
fun(arr, 5, 2) returns arr[1] + fun(arr, 3, 3)
fun(arr, 3, 3) returns arr[2] + fun(arr, 1, 4)
fun(arr, 1, 4) returns arr[4]
The output will be:
7
5
3
1
2
6
16
24
24
If you're still having any doubt then please feel free to ask in the comment section.
Get Answers For Free
Most questions answered within 1 hours.