DEV Community

loading...

Time complexity is actually easy!!

Dilsha vijay
Hi people I am dilsha vijay , currently a final year student at KPR Institute of Engineering and Technology.
・1 min read

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

Discussion (0)