DEV Community

Dilsha vijay
Dilsha vijay

Posted on

3 2

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay