This video provides a remarkably clear distillation of algorithmic efficiency, turning abstract math into intuitive logic. It is an essential primer for mastering code scalability without the burden of rote memorization.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Mastering Time Complexity: O(1) to O(n!) Explained Simply || Coding HivesAdded:
Hi everyone, welcome back to coding heist. Today we are going to understand something that scares a lot of people at first that is time complexity. But here is the thing, it is not that much hard.
It's just explained badly most of the time. So in this video I'm not going to ask you to memorize anything. I will show you how it actually comes step by step. So by the end of this video you will look at the code and say yeah I know exactly why this is order of n order of login or even order of two to n. Okay. So first we will understand what is meant by this time complexity. I want to keep it very simple. So time complexity means how the number of steps grows when input grows. I mean not seconds or not milliseconds. Just if input becomes bigger how much more work we do. Okay that is the definition we can say in a simple 10. So whenever you see a code just ask how many times is this running? That's it. That's our whole game. Okay. So first we will understand about order of one that is constant time and complexity.
So here I will write a line of code. You can see here uh array is equal to uh 10 comma 20 uh comma 30. Okay. Now I just want to print array of zero. That is I want the first element to print. Okay.
So here for that I can write print array of zero. Okay. So here you can see that this will run only one time right can see here this will run only one time right that is why we are saying the time complexity here is order of one player runs once like even if the array has three elements or 1 million elements still just one step so we can simply say this is order of one I think it is very clear for you there is no confusion right now we will of when is also known as linear time complexity. Okay, linear time complexity. I will write a code and I will explain that one also for i in a range of n then I want to print those all i okay so here you can see for example if I'm uh writing like 10 then what will be the output you can see here we are getting zero till 9 that is it runs 10 times right so this loop runs one time if n is equal to 1 or for example I gave here 10 right so I will write one more thing here for example I write here n is equal to 10 then it will run 10 times right so here this is this will the same output 10 times and if I'm giving here one then it will run only one time right this is the previous um output I will comment this one okay so Here this loop runs 1 times if n= 1 10 times if n= 10 n * if n= n.
Okay. So that is why here the steps n steps it will take right. That is why here the time complexity is order of n.
Now the next one that is order of n².
Okay. I will write sample code here. I will just make this all things comments.
So here what we are going to do is we will write two loops. I will explain this don't worry. For i in a range of n then I will write a inner loop for j in a range of n.
Then what I'm going to do is I will just uh write print i, j. Okay. So here I will write n is equal to five. Okay. So now when if I'm running this code, you can see here it will run till 5 into 5 times. You can see here. Okay. So this means the outer loop runs n times. Okay.
So here you can see first it took the number zero right then coming to the inner loop. So this inner loop will come with the 0 1 0 sorry 0 0 1 0 2 03 04 like that I mean n minus one times so here this one five times the same for 5 into 5 that is why this time complexity is 5 into 5 that is n into n that is n² okay so here outer loop takes n times inner loop n times for each outer loop okay so total we are getting n into n that is n². Clear? First we will try to understand order of uh login. Okay, this is the one most people are confused I think. So we will start with the symbol code here. For example, I'm doing like this. I is equal to n. Okay. So I'm assigning a variable like this. Then I'm writing a loop uh while I greater than 1. Okay. So here what I'm going to do is I will just make like this I is equal to um I by 2. Okay. So here I will explain this with an example. Okay. I'm assigning N as 16. Okay. So the value of N is 16. Initially uh the value of N is 16. Okay. And whatever uh code is doing every step it divides by two. Right? So here I is equal to I by 2. So initially I n is 16 and I will be 16 step I will be 8 right I mean 16 by 2. So here this will be 8 and in the next step it will be 8 by 2. So the answer is uh like four and the next step four by two and in the next step it is 2x2. So the answer is one right? So you can see here it works four times right and I I mean every step we divide by two four times I took four divisions to reach one. Okay. So instead of dividing again and again we can write it in one line.
Okay. I mean we divided n I mean n here 16 by 2 k*.
So here we know we divided this 16 four times right in this example. First you understand this example properly then only we will get idea how this law came there. Okay. So the initial number was 16 and we divided until it reaches one.
So we divided by two and by checking by analyzing this example we found that we divided this numbers in 16 four times.
Right? I mean two four times. We did like this. N uh division 2 into 2 into okay. So this much four times we did. So commonly we can say it as like for example this is in this example we know this is four time only. So if we don't know the number then we can say initially we can assign like k * okay this is k * okay so that means we divided this number n 2 to k * okay you got it how we write like this this number 16 we divided with two until it reaches one so in this example this we know four times but every time we don't know how is the input right like Maybe some other input will come here. So according to that the value will change. So we we need to make a generalized equation that is why we we are writing like this here n division 2 into 2 into 2 etc until k times. Okay.
So we can commonly write like this n by 2 to k. Clear? As we mentioned light we will stop when we reach one right. So we will write like this. We will stop when we reach one. So, so when n /x 2 to k is equal to 1 that means we stop when we reach one. Okay. Now some basic math only just to focus on here like we will rearrange I I mean I don't want this denominator. So for that I can multiply this 2 to k in the both sides. Okay I mean left and right side. So this will go on. So we will get something like n is equal to 2 to k. Okay. I'm just explaining the mass behind this login thing. Okay. So now we got like this n= 2 to k. So we didn't assume simply n is equal to 2 to k. We got it from the repeated division. You need to be clear with that. Okay. We got this much only.
But here there is no log or something.
Right? So how we can say uh we got this login from here. Okay. So now you can ask we got n= 2 to k. So what power of 2 gives me n? You can ask like this what power of 2 gives me n. That question itself is the definition of log. I believe you people learned log in your school studies. So here this question what power of two gives me n. That is the question itself is the definition of log. Okay. So that also I will explain here with one more example. uh you can I will write here for example if you're write writing like this 2 to 3 is equal to you know right 2 to 3 is 8 okay now that means we are writing like this log of 8 log of 8 in the base of 2 is that will be three this is the logic here you can see this one you need to understand so example if we are writing 2 cq is equal to 8 then we are saying log of 8 in the base of 2 is 3. Okay. So the same logic coming here. What is the logic? If n is = 2 to k then k we can write like this k is equal to uh log of n in the base of two. Okay. This is how we got uh this login as the time complexity layer. So log doesn't mean anything scary. It just means what power should I rise to to get n. Okay. So that's it. Now we will understand order of n login.
I will make it this all things comments.
If you understand login then order of login is not that much hard. So you know if for i in a range of n if I'm writing like this uh then I'm simply assigning one more variable j is equal to n then here the same uh code I'm writing like this while j greater than 1 then uh j is equal to j by 2. Okay. So here you know that we have a loop right for i in range of n.
So that means uh we have uh order of n here order of time order of n * n times we need to execute this sentence. Okay.
And this one we already understood this is time complexity of this is login. So we got order of n login as time complexity. So here the outer loop runs n times and the inner loop runs login time. So we are getting as a total order of n login. clear now I will uh this is not a 2 end this is 2 r to n but here there is no way to write like rise to that is why I'm writing here like this so that I will explain with an example here I have a function just a simple function f okay with an argument n and here what I'm going to do is if n equal to0 I will simply return okay otherwise I will call the function two times okay so I [clears throat] will write like this uh f of n minus one then again I'm calling the function f of n minus one okay so here when I'm calling this function it calls again two times you can see here so here each call creates two new calls okay so for example we can say in the level one I mean the first call it will call only one time and the next time it will call two times and the next time it will call four calls and next time it will call eight calls okay so here what What is happening is each level multiplies by two after n levels that means 2 to n. Okay. That is why we got time complexity as 2 to n. Okay. So whenever step splits into two we can write the time complexity as order of 2 to n. Clear? And the next one is um order of n factorial. Okay. This happens when we try every possible arrangements.
Imagine you have three friends for example A, B, C and you want to arrange them into a line of code. Okay. So here we have uh three friends I mean A uh B and C. Okay. We need to arrange them. We need to fix the first letter. Okay. So that one we can write in three ways.
Right. First we can start with the A and the second pattern we can make with B.
Right? Third pattern we can make with C.
Okay. So we can make uh three patterns with the first position. Okay. Now coming for the second spot. Okay. With A we can make uh A B and AC. Right? And with B we can make uh B A and BC.
B A and BC.
And with C we can make uh CA and CB.
Okay, got it. For A, we got two choices each. Okay, now for the last spot, I mean the last one when we are going for AB, we are getting ABC. Okay. And with the AC AB and with the BA B A C and BC BCA and CA B. So for each one we are getting only one only one choice only.
It will be here C and here it will be we can say B and here we can uh add C. Here we can add. So here we start the ABC. Then for each I have choices for the next S and the last one is fixed. So three then two then one right. So total we will be getting six only right.
So that is why we are telling this as a factorial. So factorial means factorial of three we can write like this uh the possibilities I mean permutation also we can say. So here like uh 3 into 2 into 1 6 as the answer. Okay.
So commonly we can say it as n into n -1 into n - 2 into etc. We are saying like n factorial. Okay.
So we can say it tries every possible way to arrange people. We'll check first arrangement, check second, check third and all the way to end. Okay.
So that also I will write a code here.
Install a library. We have I think that is iter tools. Okay. So iter tools. Then we need to import uh permutations. Okay.
Permutations.
Now what I'm going to do is I'll write like this for P in permutations. So it will show the uh possibilities. Okay.
Permutations.
then we can pass the string here. Um here I will pass our ABC only and then just simply I'm going to uh print this.
Okay. So I will make this comment. I think that is better.
Okay. Now we will uh try to run this code. You can see here you will get uh sorry you will get uh six permutations ABC.
Yeah, I got a mistake. You here want this one and we need to give the indentation here. Um, now I will run this code.
You can see this is our output like a b c a c b a c b c a c b cb. Okay. So whenever code tries all possible arrangements then the time complexity will be order of n factori. Okay. So arranging everything in all possible ways that means factorial. So we learned log n which grows very slow. N is like a normal growth. Then n² is very fast.
Then 2 is like an explosion and n factor is impossible for big like big inputs.
Okay. So you need to understand if there is no loop then the time complexity is order of one. Okay. If there is one loop then time complexity is order of one. If there is two loops nested loops then the time complexity is order of n². Then there uh we are dividing by two I mean we are decreasing the input like dividing by two we can say order of login. There if there is a loop and a divide then we can say order of n login and if you're splitting into two then we can say order of 2 to n and we are arranging uh like the permutation we can say the time complexity is order of n factorial okay so now you don't need to memorize anything just look at the pattern is it repeating is it shrinking is it doubling then you will know the answer right so that's it for today's video thank you for watching have a nice
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











