edited by
709 views
2 votes
2 votes

please explain answer given c

edited by

2 Answers

1 votes
1 votes

The question can be titled ambiguity x 100 :p

What I feel the function in else part should be read as f(counter) instead of if (counter).

Lets analyze the code

  • The for loop will run n times Θ(n)
  • if last elements of array is 0 and rest of the elements are continuously 1, Function f can have maximum complexity of Θ(n-1) only . In that case the function f will run hardly a few times. Hence we can neglect it.
  • If array has 0 and 1 frequently present, then f will be called frequently, but counter value will be small. Hence complexity of f will be small. So we ca neglect complexity of f in this case also

In short, we need to consider complexity of for loop only.

Thus answer will be c ) Θ(n)

0 votes
0 votes
As array A[1...n] having n elements. For loop will run n times. It is not depending on If and else condition here.

So, as for loop is running n times time complexity is O(n).

Related questions

1 votes
1 votes
1 answer
1
Markzuck asked Jan 6, 2019
500 views
Please show the ideal way to deal with such comparisons as I am getting g>=f IN genral what logic shall be followed to analyse such complex comparions?
0 votes
0 votes
1 answer
2
Markzuck asked Dec 29, 2018
791 views
cant we write the recurrance relation for bar() as T(n) = 5T(n-1) + c,like cant we take both the recurrance call as combined as both have same parameter?and if not, then ...
1 votes
1 votes
0 answers
3
1 votes
1 votes
1 answer
4