There are times when I want to predict the time complexity or space complexity of the algorithm and want to check whether it fits into the given constraints or not. It always looks like that the approach is correct but there are some architecture-level problems or rather concepts that I don't know very well. In each of those cases,
It is given that the problems need to solve in a time limit of 1 or 2 seconds and the memory limit of 256 Megabytes or 512 Megabytes. When do we need to consider those constraints while computing the complexities?
What should be the maximum size of the array that we can declare?
When we need to use the array or the set.
Which time complexity fits best into the given constraints.
In case of double for loop. How do we need to access the matrix (generally in DP problems) to make sure that it will not exceed the time limit? How should I write for loops bigger one outside or the smaller one?
How writing array outside of the main helps us to reduce the time complexity?
Example CSES Problem:
TLE Code: https://cses.fi/paste/c63f02b96710bdd1164250/
ACCEPTED CODE: https://cses.fi/paste/4098290d90c8bb29164255/How to use the Modulo operator effectively to reduce the time complexity?
Example CSES Problem:
TLE Code: https://cses.fi/paste/bd61629529d99d8b163f51/
Modulo is written inside both loops.
ACCEPTED CODE: https://cses.fi/paste/753f06b17cbe2b761640d5/
Modulo is written outside of the inner loop.
I need help to understand the exact mathematics behind the calculation of time and space.