Time Complexity with Simple Examples (original) (raw)

Last Updated : 19 May, 2026

Imagine a classroom of 100 students in which you gave your pen to one person. You have to find that pen without knowing to whom you gave it.

Here are some ways to find the pen and what the O order is.

Is the Time Complexity Same as Time of Execution?

The Time Complexity is **not equal to the actual time required to execute a particular code, but the number of times a statement executes.

**For example: Write code in C/C++ or any other language to find the maximum between n numbers, where n varies from 10, 100, 1000, and 10000. For Linux based operating system (Fedora or Ubuntu), use the below commands:

To compile the program: **gcc program.c – o program
To execute the program: **time ./program

You will get surprising results i.e.:

So, we can say that the **actual time required to execute code is machine-dependent!

**What is meant by the Time Complexity of an Algorithm?

**Example 1: Consider the below simple code to print Hello World

C++ `

#include using namespace std;

int main() { cout << "Hello World"; return 0; }

C

#include <stdio.h>

int main() { printf("Hello World"); return 0; }

Java

import java.io.*;

class GFG { public static void main(String[] args) { System.out.print("Hello World"); } }

Python

print("Hello World")

C#

using System;

public class GFG{

static public void Main (){

    // Code
      Console.WriteLine("Hello World");
}

}

JavaScript

console.log("Hello World")

`

**Time Complexity: In the above code “Hello World” is printed only once on the screen.
So, the time complexity is **constant: O(1) i.e. every time a constant amount of time is required to execute code, no matter which operating system or which machine configurations you are using.

**Example 2:

C++ `

#include using namespace std;

int main() { // Here n is assumed to be user input int i, n = 8; for (i = 1; i <= n; i++) { cout << "Hello World !!!\n"; } return 0; }

C

#include <stdio.h> void main() { // Here n is assumed to be user input
int i, n = 8; for (i = 1; i <= n; i++) { printf("Hello World !!!\n"); } }

Java

class GFG {

public static void main(String[] args)
{
    // Here n is assumed to be user input
    int i, n = 8;
    for (i = 1; i <= n; i++) {
        System.out.printf("Hello World !!!\n");
    }
}

}

Python

Here n is assumed to be user input

n = 8 for i in range(1, n + 1): print("Hello World !!!")

C#

using System; public class GFG {

public static void Main(String[] args)
{
    // Here n is assumed to be user input
    int i, n = 8;
    for (i = 1; i <= n; i++) {
        Console.Write("Hello World !!!\n");
    }
}

}

JavaScript

// Here n is assumed to be user input let i, n = 8; for (i = 1; i <= n; i++) { console.log("Hello World !!!"); }

`

Output

Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!!

**Time Complexity: In the above code “Hello World !!!” is printed only **n times on the screen, as the value of n can change.
So, the time complexity is **linear: O(n) i.e. every time, a linear amount of time is required to execute code.