Let A[1, …, n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:
The complexity of this program fragment is
(B) Ω(nlog n) and O(n2)
Explanation: Please note that inside the else condition, f() is called first, then counter is set to 0.
Consider the following cases:
a) All 1s in A: Time taken is Θ(n) as only counter++ is executed n times. b) All 0s in A: Time taken is Θ(n) as only f(0) is called n times c) Half 1s, then half 0s: Time taken is Θ(n) as only f(n/2) is called once.