Skip to main content

time_complexity

Time Complexity

for (int i = 1; i <= n; i += 2) {
System.out.print(i);
}

Explain - Lopp will run N times with increment of +2. So total number of interation will be N/2.

Answer - Time complexity is O(N/2) = O(N) (removing constant)

def solve(N, M):
for i in range(1, N + 1):
if N % i== 0:
print (i)
for i in range(1, M + 1):
if M % i = 0:
print (i)

Explain - Two loop will run N and M times. Each loop having nested loop that will

def func(n) :
S = 0
for i in range(1, 101):
s = s + i
return s
  1. concat a string will increse time complexity because string is immutable.