DSA : Day 2
Anushkaa Ambuj
SWE Intern @Johnson Controls | ML Intern @Capgemini | Final Year @IIT Jodhpur | Computer Science | Machine Learning | Web Development
Understanding Problem Constraints and Time Complexity
Happy Learning! Happy Programming!
Topics
1?? Relation of Constraints and Data Types
2?? Relation of Constraints and Time Complexity
3?? Time Complexity
Relation of Constraints and Data Types
??Range of Data Types
??Case Study
Will the code work upon the following constraints?? If not, at which position should the data type be changed?
??1<N<10^5 and -10^4<=arr[i]<10^4
??1<N<10^4 and -10^6<=arr[i]<10^6
??1<N<10^5 and -10^9<=arr[i]<10^9
??1<N<10^5 and -10^11<=arr[i]<10^11
??1<N<10^10 and -10^6<=arr[i]<10^6
??1<N<10^10 and -10^11<=arr[i]<10^11
Relation of Constraints and Time Complexity
??Analysis
In general, let us consider the processor is of 1GHz and limit of runtime is 1 sec.
Then, # instructions that can be executed = 10^9
Considering, each iteration performs 10 instructions.
Therefore, # iterations in total = 10^8.
Hence, for any solution, the number of iterations should be less than 10^8.
For example, if each testcase requires 'M' time, then for 'T' testcases,
Total # iterations = T*M , which should be less than 10^8.
??Case Study
Let us consider the problem below for analyzing.
??Sol A: O(N) --> Applying loop and multiplying A for B times
??Sol B: O(logN)
Time Complexity
O(1) < O(logN) < O(N) < O(NlogN) < O(N^2) < O(2^N) < O(N!)