Blog Details

ptitle-particle1
ptitle-particle2
ptitle-particle3
ptitle-particle4
time complexity big o notation O1 On Ologn explained

What is Time Complexity? Big-O Notation Explained Simply

Introduction

When writing programs, it’s not just important to make them work — they should also run efficiently. Time complexity helps us measure how fast an algorithm runs as the input size increases.

Understanding time complexity is essential for writing optimized code and performing well in coding interviews.


What is Time Complexity?

Time complexity is a way to measure how the execution time of an algorithm grows with the size of the input.

It helps us analyze the efficiency of code.


What is Big-O Notation?

Big-O notation is used to describe the performance of an algorithm in terms of time or space.

It shows how the runtime increases as input size increases.


Common Time Complexities

1. O(1) – Constant Time

The operation takes the same time regardless of input size.

Example:

function getFirstElement(arr) {
  return arr[0];
}

2. O(n) – Linear Time

The time increases linearly with input size.

Example:

function printArray(arr) {
  for (let i = 0; i < arr.length; i++) {
    console.log(arr[i]);
  }
}

3. O(log n) – Logarithmic Time

The time grows slowly as input size increases.

Example: Binary Search

function binarySearch(arr, target) {
  let left = 0, right = arr.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) return mid;
    else if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
  }
  return -1;
}

Why Time Complexity Matters

  • Helps write efficient programs
  • Reduces execution time
  • Important for large-scale applications
  • Crucial for coding interviews

Real-Life Analogy

  • O(1): Picking a book from the top of a stack
  • O(n): Searching a book in a pile one by one
  • O(log n): Searching in a sorted book index (divide and search)

Tips to Improve Code Efficiency

  • Avoid unnecessary loops
  • Use efficient algorithms
  • Learn data structures
  • Practice optimization

Conclusion

Time complexity helps you understand how efficient your code is. By learning Big-O notation, you can write faster and better programs.

Start learning with Mango Engineers and build strong problem-solving skills.

Leave A Comment

We understand the importance of approaching each work integrally and believe in the power of simple.

Melbourne, Australia
(Sat - Thursday)
(10am - 05 pm)
Cart

No products in the cart.