CONSTRAINTS

How do I apply constraints? Should I use if-else or use datatypes? Or any other way. please help!

You don’t have to do anything - they are essentially “promises”/ “guarantees” made to you by the Problem Setter/ Tester. See e.g. my post about Subtasks and Constraints.

1 Like

constraints are to set data types correctly. Not writing if else statement. Analyse the constraint before writing your code and set data types accordingly. To understand it further lemme take few examples.
1 <= N <= 1000 - in this case max value for N is 1000 and which can be stored in int.
1 <= N <= 10^6 (1000000) - now you cant store N in normal int, so you need to declare a datatype which can stores 10^6 (long int can be used here).
Likewise constraints are used to set the proper data types for your program.
Wait this is not the end there is one more thing, which is time limit. To know about that will take an example.
Your program has to nested loops in which outer loop runs for N times and inner loop runs for Q times. And time complexity of your program will be O(N x Q), where N can be 1<= N <= 10^5 and Q can be 1<= N <= 10^5. CPU process only 10^8 inputs per second. But your program runs for O(N x Q) which is O(10^5 x 10^5) = O(10^10) and time limit given to the problem is 1 sec. Now CPU automatically terminates your program because your program takes more time then the given constraint.
Concluding constraints used to set right data types as well as to make sure that your program runs in the given time limit.

Hope this helps…
Happy Coding…

6 Likes