What is the time complexity for this? And what's the Big-O?
int sum = 0;
for (int i = 0; i<= 20; i+=2)
for (int j = 0; j<= i; j++)
sum+= 2i * j ;
System.out.println (sum);
System.out.ptintln(“I = ” + i + “ J = ” + j);
What is the time complexity for this? And what's the Big-O?
int sum = 0;
for (int i = 0; i<= 20; i+=2)
for (int j = 0; j<= i; j++)
sum+= 2i * j ;
System.out.println (sum);
System.out.ptintln(“I = ” + i + “ J = ” + j);
Since nothing depends on any input and the number of iterations is always fixed, this has constant-time complexity or O(1). Asymptotic runtime complexity is always a property of a function describing how the number of operations change in relation to the input size.
There is no input and the function always takes the same time to execute, hence O(1).
Since the main loop's iterations does not increase/decrease drastically with change in input, the code is O(N).
You can give whatever limit you want to the I<=20 iterator, and the iterations will grow linearly