This is because the loop #1and loop #2use the same count variable iduring the comparison.
At the end of the 2nd cycle using, the ivalue iis n, which also causes it to exit the outer loop. One cycle is completely redundant.
#include <stdio.h>
int main(){
int x = 0;
int n = 20;
int i, j;
for(i=0; i<n; i++)
{
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
x++;
}
}
}
printf("%d", x);
}
O(N^2)Everything is done in time.
Example
source
share