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:
Case 1 :-
counter = 0;
for (i = 1; i < = n; i++)
{
if (A[i] == 1)
counter++;
else {
f(counter);
counter = 0;
}
}
Case 2 :-
counter = 0;
for (i = 1; i < = n; i++)
{
if (A[i] == 1)
counter++;
else {
counter = 0;
f(counter);
}
}
The complexity of this program fragment is
(A) Ω(n2)
(B) Ω(nlog n) and O(n2)
(C) θ(n)
(D) O(n)
the question is how do i know that when if statement or else statement is used and when the f(m) function is called , How do i approach it? i can consider the cases when only if is executed or only else but what about general case when if statement is executed sometimes and else statement sometimes
We can start by looking at the easy case, case 2. Clearly, each time we go through the loop in case 2, one of either 2 things happens:
we go through the loop n times, each time takes practically O(1) time, bada-bing, bada-boom, it takes O(n) (or O(n * lg(n)) if you're being pedantic and using variable-length integers).
Case 1, on the other hand, requires a little bit of mathematical thinking.
The bit strings that take the shortest amount of time in Case 1 are obviously
11111....11111
,000....000
,000...0111...111
, or similar. All of these take θ(n) time to complete, establishing a lower bound for case 1. Now, we need to establish a worst-case scenario.Without going into the rigor of a proper proof, it's pretty straightforward to assert that the worst-case bit strings look like this:
A bit string of the above form with length 100 would have 99 1's, and therefore would need
99 + 99
time units to complete. A string of length n clearly needs2(n - 1)
time units to complete.This is clearly still linear in n, so case 1, even in the worst case scenario, is θ(n).
Because both case 1 and case 2 are θ(n), the problem is θ(n).
If you still need to be convinced that
11.....110
is the worst case bit string, consider this:As a matter of principle, I don't answer poorly asked homework questions on stackoverflow.
After talking to coder101 in chat, however, he/she showed me that this is NOT a homework problem, but is instead a problem that was retrieved from an online database here, which is meant to provide "mock tests for geeks". This looks like a challenge that coder101 bestowed upon themselves, and while it could be a better question, I don't think it's that bad.