Q.1 :-What is the time complexity of following code:
int i, j, k = 0;
for (i = n/2; i <= n; i++) {
for( j = 2; j <= n; j = j+ 1) {
k=k+ n/2;
}
}
Answers
Answer:
The time complezity is O()
Explanation:
The outer loop runs n/2 times
The inner loop runs logn times.(2^k = n => k = logn).
Now looking at the value of k in inner loop, n is added to k, logn times as the inner loop is running logn times.
Therefore total time complexity is inner multiplied with outer loop complexity which (n for outer and nlogn for inner) n*logn.
Therefore the value of k after running the inner loop one time is n^2logn.
The length of the input determines how long it takes an algorithm to run, which is known as temporal complexity. It calculates how long it takes for each algorithm's code statement to run. It won't look at an algorithm's overall execution time. Instead, it will provide details regarding the variation (increase or decrease) in execution time when an algorithm's number of operations (increase or decrease). Yes, as stated in the definition, the time required depends solely on the length of the input.
See more:
https://brainly.in/question/44013424
#SPJ1