128k views
3 votes
What is the Big-Oh of the following computation? int sum = 0; for (int counter = 1; counter < n; counter++) sum = sum + counter;

User Rcorre
by
4.2k points

1 Answer

4 votes

Answer:

The Big-O notation of the algorithm is O(n)

Step-by-step explanation:

The declaration and update of the integer variable sum is a constant of O(1). The for loop statement, however, would repeat relative to the size of "n", increasing the counter variable and updating the sum total by the counter.

User Ninesalt
by
4.2k points