online calculate time complexity

#include <stdio.h>

void exampleFunction(int n) {
    int i, j;

    for (i = 0; i < n; i++) {
        printf("i: %d\n", i); // O(1)
    }

    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("i: %d, j: %d\n", i, j); // O(1)
        }
    }

    int k = 0;
    while (k < n) {
        printf("k: %d\n", k); // O(1)
        k = k * 2;
    }
}

int main() {
    int size = 10;
    exampleFunction(size);

    return 0;
}

Explanation:

  1. The first loop runs 'n' times, and the printf statement inside it is a constant time operation, so the time complexity of the first loop is O(n).

  2. The nested loops run 'n' times each, resulting in a total of n * n iterations. The printf statement inside the nested loops is a constant time operation, so the time complexity of the nested loops is O(n^2).

  3. The while loop runs until 'k' becomes greater than or equal to 'n'. In the worst case, 'k' doubles in each iteration, so the number of iterations is log(n) (base 2). The printf statement inside the while loop is a constant time operation, so the time complexity of the while loop is O(log n).

Overall, the time complexity of the exampleFunction is O(n + n^2 + log n), but in big-O notation, we consider the dominant term, so the final time complexity is O(n^2).