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

Oldest comments (0)