MCQ IN COMPUTER SCIENCE & ENGINEERING

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; } }
A
O(N)
B
O(N*log(N))
C
O(N * Sqrt(N))
D
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.