A Beginners Guide to Time Complexity in Programming.

A Beginners Guide to Time Complexity in Programming.

If you are new to the world of programming, you may have heard words like Time Complexity, the Big(O) Notation, Time Constraints of the question, etc. Today, we will see what actually Time complexity means and how understanding it will make you a much better problem-solver and save you from the Time Limit Exceeded errors.

Introduction

Time Complexity of an algorithm is the amount of time taken by an algorithm to run as a function on a particular input size. It is assumed that one operation can be executed in a constant time c, and then the c is multiplied with the input size N. Sounds complex? Let's have a look at an example.

tcomplex.png

An Example

Suppose there is a vector of size N. If you have read my article on Basics of Vectors, you would know that the time required to traverse a vector is O(N). How did we get this O(N). Let's see.

As discussed above, a single operation takes some constant time c. Now since we want to traverse the array, it means that we want to go to all the elements one by one. This means we must do N operations on the array. So the overall complexity of traversing an array turns out to be O(N*c). Here c is a very small constant hence it is neglected and we have a time complexity of O(N).

I hope this example makes you understand how a time complexity of an algorithm is calculated.

Co-relation between Time Complexity & Constraints.

If you have started solving DSA questions on sites like Hackerrank, Leetcode, etc, you will see that in every question, there are some mathematical constraints like this

Screenshot (81).png

  • Here, nums is an input array, nums[i] is the element inside the array, and target is some other variable that is required in the question.
  • You can see that the size of the array ranges from 0 to 10^4. The range of the element inside it ranges from -10^9 to +10^9 and same for the target.
  • Suppose, I wanted to traverse the array, so my time complexity would depend only on the size of the array.
  • Suppose, I wanted to reduce t the target variable value to 1, so my time complexity would only depend on the value of target variable.
  • It is very important to understand that which variables are actually impacting your time complexity.

Escaping the TLE.

If you are fed up of the Time Limit Exceeded error and need to know what complexity your algorithm must be to pass all the test cases, have a look at this table below.

Screenshot 2022-02-07 120005.png

This table tells you the worst time complexity to pass all the test cases for a particular input size. So suppose, you wanted to find two numbers in an array whose sum is 0, you would need to do a nested for loop. Something like this

  for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(i!=j && arr[i]+arr[j]==0)
            {
                cout<<i<<" "<<j<<endl;
            }
        }
    }
  • The outer loop runs for n times and the inner loop runs for n times as well. So the overall time complexity of this code turns out to be n*n i.e n^2.
  • So if the constraints of n were less than 10000, this algorithm would pass all the test cases. But, if the n would be bigger than 10K, you will get a TLE.
  • This is how you analyze the complexity of your algorithm and with the help of the table you can see if it will pass all the test cases or not.

Conclusion

So this sums up our article on basics of understanding time complexity. Time complexity is a very vast topic. We have just scratched the surface of time complexity in this article.

If you want a deeper understanding of the Big(O) notations, then i recommend you to go watch this video.

After reading this article, you will be now able to figure out what complexity your code is and will it pass all the test cases for a given input or not.

Did you find this article valuable?

Support CompSciWithIyush by becoming a sponsor. Any amount is appreciated!