Lot of students get confused while understanding the concept of time-complexity, but in this article we will explain it with a very simple example:

Imagine a classroom of 100 students in which you gave your pen to one person. Now, you want that pen. Here are some ways to find the pen and what the O order is.

**O(n ^{2}):** You go and ask the first person of the class, if he has the pen. Also, you ask this person about other 99 people in the classroom if they have that pen & So on,

This is what we call O(n

^{2}).

**O(n):** Going and asking each student individually is O(N).

**O(log n):** Now I divide the class in two groups, then ask: “Is it on the left side, or the right side of the classroom?” Then I take that group and divide it into two and ask again, and so on. Repeat the process till you are left with one student who has your pen. This is what you mean by O(log n).

I might need to do the O(n^{2}) search if only one student knows on which student the pen is hidden. I’d use the O(n) if one student had the pen and only they knew it. I’d use the O(log n) search if all the students knew, but would only tell me if I guessed the right side.

**Another Example**

Time Complexity of algorithm/code is **not** equal to the actual time required to execute particular code but the number of times a statement execute. We can prove this by using time command. For example, Write a code in C/C++ or any other language to find maximum between N numbers, where N varies from 10, 100, 1000, 10000. And compile that code on Linux based operating system (Fedora or Ubuntu) with below command:

gcc program.c –o program run it with time ./program

You will get surprising results i.e. for N = 10 you may get 0.5ms time and for N = 10, 000 you may get 0.2 ms time. Also you will get different timings on different machine. So, we can say that actual time require to execute code is machine dependent (whether you are using pentium1 or pentiun5) and also it considers network load if your machine is in LAN/WAN. Even you will not get same timings on same machine for same code, the reason behind that the current network load.

Now, the question arises if time complexity is not the actual time require executing the code then what is it?

**The answer is :** Instead of measuring actual time required in executing each statement in the code, we consider how many times each statement execute.

For example:

`#include <stdio.h> ` `int` `main() ` `{ ` ` ` `printf` `(` `"Hello World"` `); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Hello World

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

**Now consider another code:**

`#include <stdio.h> ` `void` `main() ` `{ ` ` ` `int` `i, n = 8; ` ` ` `for` `(i = 1; i <= n; i++) { ` ` ` `printf` `(` `"Hello Word !!!"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Hello Word !!!Hello Word !!!Hello Word !!!Hello Word !!! Hello Word !!!Hello Word !!!Hello Word !!!Hello Word !!!

In above code “Hello World!!!” will print N times. So, time complexity of above code is O(N).

Source : Reddit

The co-author of this article is **Varsha Lokare.**

## Recommended Posts:

- A Time Complexity Question
- An interesting time complexity question
- Time Complexity of Loop with Powers
- Time Complexity of building a heap
- Practice Questions on Time Complexity Analysis
- Time Complexity where loop variable is incremented by 1, 2, 3, 4 ..
- Time complexity of recursive Fibonacci program
- Time Complexity Analysis | Tower Of Hanoi (Recursion)
- Python Code for time Complexity plot of Heap Sort
- Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially
- Cyclomatic Complexity
- What does 'Space Complexity' mean?
- Knowing the complexity in competitive programming
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Measure execution time with high precision in C/C++

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.