0

Find how many times the this code will run in terms of n

for (i = 1; i < n; i ++) for(j = n; j > i; j --) I am supposed to calculate the number of times it will run in terms of n. And find the upper bound. I know the outer for loop runs n - 1 times, but am confused how to find the amount of times the inner loop runs

5th May 2020, 10:13 PM
Fare Jare
Fare Jare - avatar
2 odpowiedzi
+ 2
Why don't you just run it like this? int i,j,n=6; for (i = 1; i < n; i ++) { for(j = n; j > i; j --) { System.out.println("hi"); } System.out.println(); } It will also run n-1 times.
5th May 2020, 10:20 PM
Avinesh
Avinesh - avatar
0
It is something like this i*(n-i) ,1*(5-1),then 2*(5-2) and so on maybe 10 times
5th May 2020, 10:21 PM
Abhay
Abhay - avatar