DEV Community

Dilsha vijay
Dilsha vijay

Posted on

 

Time complexity is actually easy!!

Time complexity in simple words can be described as the number of steps executed in order to reach the final output.It depends on the ram , other simultaneously running threads and so on.

The first type is O(1)
So it means for any number of input the output is constant single step. For example

n=int(input())
if(n>0)
   print("Hello World")
Enter fullscreen mode Exit fullscreen mode

Second one is O(n)
This means that the number of steps to get to the final result is equal to the input of the program.

n=4
for(int i=0;i<n;i++){
   System.out.println("Hello world");
}
Enter fullscreen mode Exit fullscreen mode

Next is O(n^2)
This can be calculated simply multiplying the length of the nested arrays

O(log(n))
Example take a binary tree for a possible of all binary representation of digits like 1 or 0... or take binary search as an example
We are dividing each time time based on the condition till we get the element we searched for So the time complexity of both these cases are logn

Thank you or reading

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesnโ€™t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.