Recursion

AP Computer Science A· difficulty 5/5

public static int m(int n) {
    if (n < 2) return n;
    return m(n / 2) + m(n / 2) + n;
}
System.out.println(m(4));

What is printed?

  • A

    4

  • B

    10

  • C

    12

    check_circle
  • D

    8

Explanation

m(0)=0,m(1)=1,m(2)=m(1)+m(1)+2=4,m(4)=m(2)+m(2)+4=4+4+4=12.

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

Related questions