Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Big o notation examples with solutions

YASH PAL, 12 May 202028 May 2024

Big o is also called an asymptotic analysis of the algorithm. it defines the complexity of an algorithm on different inputs size and finds the order of the algorithm using the function. so today we are going to learn how to calculate big o notation over the for loops, functions, and operators using the different types of examples.

there are some rules to find the big o notation of an algorithm. so if you don’t know these rules then you should read this post first.

  • Big O notation in algorithms analysis

Big O notation examples

Big o asymptotic notation examples

In the above example, we have one assignment operator and three for loops. but one for loop is only executed 6 times. so the running time of the third loop is constant so we don’t consider that.

next first for loop maximum executes for n-2 times and the second for loop executes for n times. so the running time of the algorithm is n + n -2.

so the algorithm is an order of n.

f(n) is O(n)

Big o asymptotic notation examples

in the above example, the algorithm has only one for loop and one input and output. but the inputs and outputs execute in constant time so we don’t consider them. 

next, the for loop is maximum executes for n times. so the complexity of the algorithm is n. and its order is n.

f(n) is O(n).

Big o asymptotic notation examples

In the above example, the algorithm has only one input output and a loop. the loop has executed a maximum of n+1 times. so the running time of the algorithm is n+1 which is nothing but the order of n. 

so f(n) is O(n).

In the above example, we have a nested for loop means in a loop we have another for a loop. and both algorithms can run a maximum of n times. so we can say the complexity of an algorithm is n*n that is nothing but n2

so the function f(n) is O(n2)

Big o asymptotic notation examples

next in the above example algorithm we have a nested for loop and in the for loop we have an assignment operator that maximum executes for n times. 

and for the operator, the running time is the order of logn. but we also have nested for loops and both can run maximum for n times. so the complexity of the algorithms is nlogn + n2

so the f(n) is O(n2)

Big o asymptotic notation examples

in the above example algorithm, we have two nested for loops. and in the first nested for loop, we have three two inners for loop and in the second we have one inner for loop. so the complexity of the algorithm is n3 + n2

so the algorithm is the order of O(n3)

Big o asymptotic notation examples

In the above example algorithm, we have one nested for loop so in simple terms the algorithm has an order of n2

Big o asymptotic notation examples

this is also the order of n2

Big o asymptotic notation examples

this is also the order of n2

Big o asymptotic notation examples

in the above example, we have a loop that can run a maximum of n times but we also have a function in the loop that can also run a maximum of n times. 

so every function has the complexity of logn. so we can say that the complexity of the algorithm is nlogn.

so the function has an order of O(nlogn).

Big o asymptotic notation examples

in the above example, we have a for loop and an if-else statement. and in any conditional statement, we select the complexity of that condition that can run maximum time. so in the if-else statement if the condition can run maximum time so we consider that. and for loop can run maximum n times. also, we have an assignment operator in the loop that can run a maximum of n times. so the complexity of the algorithm is nlogn + n2

so the algorithm has the order of O(n2)

Big o asymptotic notation examples

in the above example, we have a loop that can run a maximum of 5 times so the whole time to run this algorithm is constant. so it’s an order O(1) algorithm.

Computer Science Tutorials computer scienceData Structure

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes