ISRO Computer Science (CSE) 2014 - Question 67

All ISRO Computer Science (CSE) papers | ISRO Computer Science (CSE) 2014

What is the time complexity for the following C module? Assume $n > 0$.\n```c\nint module(int n)\n{ \n if (n == 1) return 1;\n else return (n + module(n - 1));\n}\n```

  • A. $O(n)$
  • B. $O(\log n)$
  • C. $O(n²)$
  • D. $O(n!)$

Answer: A. $O(n)$

Explanation: Recursion depth is n. Each call does constant work. $T(n) = T(n-1) + C \Rightarrow O(n)$.

| | All questions of ISRO Computer Science (CSE) 2014