please explain exercise 1 unable to understand from video
Cannot understand exercise 1
we are required to find time complexity
so
we see
that i runs from [0,n-1] so this loops runs N times
thenwe have j from [i+1 , k]
so in first iteration when i =0 jth loop runs k times
then when i = 1 j loop iterates k-1 times
then k-2 times and so on
so in total jth loop runs
k*k-1 * k-2 … 1
that is sum of k numbers
k * (k+1) /2
that gives time complexity O(k^2)
and then O ( N ) times is the outer loop
so total would be O ( N+ k^2)
it would had be O ( N * K )
had the inner loop been j = 0 ; j <= k ; j++
but since here the loop is
j= i+1 ; j <= k ; j++
so no of inner iteration reduce with increasing i hence
we have this time complexity
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.