# Knowing the complexity in competitive programming

Prerequisite: Time Complexity Analysis

Generally, while doing competitive programming problems on various sites, the most difficult task faced is writing the code under desired complexity otherwise the program will get a TLE ( **Time Limit Exceeded** ). A naive solution is almost never accepted. So how to know, what complexity is acceptable?

The answer to this question is directly related to the number of operations that are allowed to perform within a second. Most of the sites these days allow **10 ^{8} operations per second**, only a few sites still allow 10

^{7}operations. After figuring out the number of operations that can be performed, search for the right complexity by looking at the constraints given in the problem.

**Example:**

Given an array A[] and a number x, check for a pair in A[] with the sum as x.

where N is:

1) 1 <= N <= 10^{3}2) 1 <= N <= 10^{5}3) 1 <= N <= 10^{8}

**For Case 1**

A naive solution that is using two for-loops works as it gives us a complexity of O(N^{2}), which even in the worst case will perform 10^{6} operations which are well under 10^{8}. Ofcourse O(N) and O(NlogN) is also acceptable in this case.

**For Case 2**

We have to think of a better solution than O(N^{2}), as in worst case, it will perform 10^{10} operations as N is 10^{5}. So complexity acceptable for this case is either O(NlogN) which is approximately 10^{6} (10^{5} * ~10) operations well under 10^{8} or O(N).

**For Case 3**

Even O(NlogN) gives us TLE as it performs ~10^{9} operations which are over 10^{8}. So the only solution which is acceptable is O(N) which in worst case will perform 10^8 operations.

The code for the given problem can be found on : https://www.geeksforgeeks.org/write-a-c-program-that-given-a-set-a-of-n-numbers-and-another-number-x-determines-whether-or-not-there-exist-two-elements-in-s-whose-sum-is-exactly-x/