Recursion

AP Computer Science A· difficulty 3/5

public static int moves(int n) {
  if (n == 0) return 0;
  return 2 * moves(n - 1) + 1;
}
// call: moves(3)

Minimum moves for 3 disks?

  • A

    6

  • B

    7

    check_circle
  • C

    9

  • D

    8

Explanation

Towers of Hanoi requires 2^n - 1 moves; for n=3, that's 7.

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

Related questions