Why to Use Pre-Computation?
A variety of questions in competitive programming requires printing solution for each Query. Test cases are designed in such a way that if a programmer computes solution again and again for each query then it may result in TLE (Time Limit Exceed).
Pre-Computation allows to store a part of solution before processing the queries and then for each query instead of calculating the solution we just need to retrieve that solution.For example, let’s say you have Q queries and each query requires to traverse an array of size N, this will result in a time complexity of O(Q*N), imagine if we can precompute and store the data in O(N) time and then process each query in O(1) time, then we can clearly see that the complexity drastically drops to O(max(Q,N)).
Contact Us