If codes are written in Python, then there is something wrong with Q11. It will run forever because n never will be 0 or negative. I think division should be replaced with floor division in the Q11:
def f(n):
ans = 0
while (n > 0):
ans += n
n //= 2;
print(ans)
In this Question the outer for loop run n times (1 to n) and inner one runs m times(1 to m )
since for each iteration of outer loop ,the inner one runs m times
when i=1 ,j= 1to m
i=2,j=1 to m and so on …
hence its time complexity is O(n*m ).
time complexity is depend on both ‘n’ and ‘m’ variable. So , time complexity should express in term of both variable. This is same as O(n+n) => O(2n) that’s nothing but O(n) but in this case we don’t know whether ‘m’ equal to ‘n’ or not. So best way to express is O(m+n)
No, because on 7,8,9 they are not increasing by i, but rather by 1. The inner loop will run n times and the outer loop will run n times, so it will be n^2. 8 is the same. For 9 when i =1 it will run n times, the n-1 then n-2 and so on. It is common knowledge that sum of natural numbers is (n^2 - n )/2