57.4k views
2 votes
In the following code fragment, how many times is the count() method called as a function of the variable n? Use big-O notation, and explain briefly. for (int i = 0; i < 3; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < j; k++) { count(); } }

User SANDHYA
by
4.7k points

1 Answer

3 votes

Answer:

The loop counts the count() function length of n-1 times with respect to n.

Step-by-step explanation:

The first and outer loop counts for two times as the variable declared in the condition counts before the iteration is made. The same goes for the other for statements in the source code.

The n represents the number length of a range of numbers or iterables (like an array).

User Paulbullard
by
3.9k points