class NestedLoop {
public static void main(String[] args) {
int n = 10; // O(1)
int sum = 0; // O(1)
int j = 1; // O(1)
double pie = 3.14; // O(1)
for (int var = 0; var < n; var++) { // 0(n)
System.out.println("Pie: " + pie); // 0(n)
while(j < var) { // 0(n)
sum += 1; // 0(n)
j *= 2; // 0(n)
}
} //end of for loop
System.out.println("Sum: " + sum); // O(1)
} //end of main
} //end of class
Here in while loop its give that it will run o(n) time which is true when var is 0,1 but when var is 2 the while condition becose true and j will increase to 2 then the condition will fail so here the loop executed two times in the smae way when var is 2 , 5, 9, 17 … loop will execute 2 times
Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers