Question Details

No question body available.

Tags

java time-complexity big-o

Answers (2)

February 11, 2026 Score: 8 Rep: 400 Quality: Medium Completeness: 50%

The outer loop for (int i = 0; i < N; i++) is O(N), after a constant threshold (1024 in your example) number of iterations are halved but this does not change complexity.

The important part of this loop for (int j = 0; j < N; j += j+1) is j += j+1 which can be evaluated to 2j + 1, after t iterations j = 2^t giving us O(logN).

Here for (int k = 0; k < N; k +=(i+j+5)) we look at k += (i+j+5)) which gives us O(N/(i+j+5)) however since j is growing exponentially the number of iterations this loop hits shrinks exponentially giving us O(logN) again this is only with taking into account values of j, by itself it is O(N/(i+j)).

So, overall the algorithm is O(N logN logN) = O(Nlog^2N)

February 11, 2026 Score: 0 Rep: 7,933 Quality: Low Completeness: 50%

For each value of i and j, the value of k will increment approximately N/(i+j+5) times. Since values of j increase as powers of 2, altogether this gives i=0N(∑j=0log N N/(i+2j+5)) iterations. The sums over i and j can be interchanged, which gives j=0log N (N(∑i=0N 1/(i+2j+5))). The inner sum is a partial sum of the harmonic series, so it can be approximated by log(N + 2j) - log(2j) = log(1 + N/2j) = log(1+2(log N - j)) ≈ log(2(log N - j)) = log N -j. The double sum is then approximately equal to N(∑j=0log N (log N -j)) ≈ N(log N)(log N + 1)/2 ≈Nlog(N)2/2. Which gives complexity O(Nlog(N)2).