COMPUTER SCIENCE AND ENGINEERING
ALGORITHMS
Question
[CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
|
What is the time complexity of following code:int a = 0;for (i = 0; i < N; i++) { for (j = N; j > i; j ____ ) { a = a + i + j; } }
|
O(N)
|
|
O(N*log(N))
|
|
O(N * Sqrt(N))
|
|
O(N*N)
|
Explanation:
Detailed explanation-1: -The answer is O(n) The outer loop runs ānā times and the inner loop only runs to ānā a single time in all the iterations combined as the value of j is never reset to 0. Therefore the answer is O(n+n)=O(n).
Detailed explanation-2: -Constant Time Complexity: O(1) When an algorithm has constant time with order O (1) and is independent of the input size n, it is said to have constant time with order O (1).
There is 1 question to complete.