The time complexity of the System.out.println(stringy);
Basically, you meant the time complexity of the code snippet above. Look, time complexity is not specifically related to one particular code or language, basically it means how much time the code line will theoretically execute. This usually depends on two or three things:
- input size
- polynomial degree (in case of solving polynomial equations)
Now in this part of your code:
String stringy = ""; while(stringy.length() < N) {// the loop will execute in order of N times System.out.println(stringy);//println will execute in order of N times too as in printing each character stringy += "X"; }
This will obviously depend on the size of the input, which, of course, is the length of the string. First, the while loop executes a little less than N (due to the condition stringy.length() < N , forcing it <= to execute it along the entire length of the string), which we can say in order N, and the line will be printed in order N , so the common code will have an O(N^2) runtime
source share