I am learning algorithms and stumbled upon this article in top coder.
This is an example from the article
int result=0; // 1
for (int i=0; i<N; i++) // 2
for (int j=i; j<N; j++) { // 3
for (int k=0; k<M; k++) { // 4
int x=0; // 5
while (x<N) { result++; x+=3; } // 6
} // 7
for (int k=0; k<2*M; k++) // 8
if (k%7 == 4) result++; // 9
} // 10
The time complexity of the while-cycle in line 6 is clearly O(N) – it is executed no more than N/3 + 1 times.
I am confused here as author says time complexity is O(N). For me it seems O(N^4). Please explain, what am I overlooking. I am only starting algorithms.
The complexity of the while-cycle in line 6 is O(N).