sir, in the last exercise we have multiplied the complexities of both inner and outer loop but in exercise 1 and 2 we have added them why is it so?
Doubt in computing complexity
hello @we_kaash
in excercise 1 ->
outer loop was going from i=0…n-1
and inner loo from j=i to k
so in 1st iteration of outer loop -> the inner loop will execute k times
so in 2nd iteration of outer loop -> the inner loop will execute k-2 times
so in 3rd iteration of outer loop -> the inner loop will execute k-3 times
…
…
so on
so in kth iteration of outer loop -> the inner loop will execute 1 times
after that k+1…n-1 inner loop will never execute that why
time complexity =k*(k+1)/2 + n-k+1
in last exercise for every iteration of outer loop inner loop is executing exeactly k times.
thats why n * k is the time complexity in last case
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.