Discussion Forum on time complexity, self referential structure (Week 2)

Answer of the question

Answer of the question

by Ahmed Ishtiak Nihal -
Number of replies: 0

ans to the ques no: 1

Time Complexity: It's a term that deals with the total execution time of an algorithm. The best time complexity ever is "Linear Time, Constant Space", which is the most efficient one ie. O(n). But not all the algorithms has a linear time complexity. That's why "the lesser the complexity is the better the algorithm".

ans to the ques no: 2

The code has got various loops to deal with. The first loop even though that's a bug as the 'for' is missing there counts a complexity of O(n+1). Cause it follows by a condition and a single loop.

In second phase there are 3 more loops. Where two of those are linked that gives us the complexity of O(n^2). And the last most loop is again a non nested one. SO, it counts  O(n) again.

As O(n^2) is greatest among all so the actual complexity is itself.

*But there is a catch. The highest complexity is always feasible in worst case. As the code's second phase deals with a condition where it executes only if it satisfies the condition so the complexity on it's best case can be as minimum as O(n+1). But in terms of total count we still can say it O(n^2).