• edited by
1,379 views
2 2 votes

please explain answer given c

2 Answers

2 2 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 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).
Position:
Show:

Related questions

2 2 votes
0 0 answers
964
964 views
1 1 vote
1 1 answer
776
776 views
Markzuck asked Jan 6, 2019
776 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 0 votes
1 1 answer
1.6k
1.6k views
Markzuck asked Dec 29, 2018
1,600 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 1 vote
1 1 answer
2.8k
2.8k views
Ramij asked Dec 20, 2018
2,831 views
O($n^2$)O(n)O(nlogn)O($n(logn)^2$