Recursion

AP Computer Science A· difficulty 5/5

public static int paths(int r, int c) {
    if (r == 0 || c == 0) return 1;
    return paths(r-1, c) + paths(r, c-1);
}
// Call: System.out.println(paths(2, 3));

What is printed?

  • A

    20

  • B

    10

    check_circle
  • C

    6

  • D

    5

Explanation

C(2+3, 2) = 10. Or compute: paths(2,3)=paths(1,3)+paths(2,2). paths(1,3)=4, paths(2,2)=6. Total 10.

Want 10 more like this — adaptive to your weak spots?

Related questions