educative.io think that the below code has complexity log2(n!) because var
increases for each value of n.
However, my associate and I think that because var
is always < n
there is no way that it could be log2(n!) because it would never take more time than if the while loop just ran off n
. The time complexity if n
were used in the while predicate is n*log2(n).
public static void main(String[] args) {
int n = 10;
for (int var = 0; var < n; var++) {
int j = 1;
while(j < var) {
j *= 2;
}
}
}