Number of Comparisions (II)

Find the number of comparisons done in the following algorithm.

1
2
3
4
5
6
7
a := 0
for i := 0 to i := n
    if (i mod 2) = 1
        a := a + 2
    else
        a := a + 1
print a

Assumptions and Details

  • \(n\) is a non-negative integer.
  • Don't count any comparison made by the for loop itself (i.e. whether or not to proceed with another loop).

Can you solve this as well?
×

Problem Loading...

Note Loading...

Set Loading...