GATE | GATE-CS-2004 | Question 82
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:
counter = 0; for (i = 1; i < = n; i++) { if (A[i] == 1) counter++; else { f(counter); counter = 0; } } |
chevron_right
filter_none
The complexity of this program fragment is
(A) Ω(n2)
(B) Ω(nlog n) and O(n2)
(C) θ(n)
(D) O(n)
Answer: (C)
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.
Recommended Posts:
- GATE | GATE-CS-2015 (Mock Test) | Question 10
- GATE | GATE-CS-2015 (Mock Test) | Question 10
- GATE | GATE-CS-2015 (Mock Test) | Question 8
- GATE | GATE-CS-2015 (Mock Test) | Question 9
- GATE | GATE-CS-2015 (Mock Test) | Question 10
- GATE | GATE-CS-2015 (Mock Test) | Question 11
- GATE | GATE-CS-2015 (Mock Test) | Question 17
- GATE | GATE-CS-2015 (Mock Test) | Question 13
- GATE | GATE-CS-2015 (Mock Test) | Question 14
- GATE | GATE-CS-2015 (Mock Test) | Question 17
- GATE | GATE-CS-2015 (Mock Test) | Question 16
- GATE | GATE-CS-2015 (Mock Test) | Question 5
- GATE | GATE-CS-2015 (Mock Test) | Question 2
- GATE | GATE-CS-2015 (Mock Test) | Question 17
- GATE | GATE-CS-2015 (Mock Test) | Question 4
Improved By : OnkarMarbhal



