 # Constraints

what are Constraints exactly and what should i do? if user try to input data that is not allowed ask him again for data or it is just to let us see the limits of input

Constraint Means the upper and lower limits of Input Data.It also means the Set of Valid Input Data.

``````1 <= T <= 1000        //Means the value of T in Input Data set  is greater than or equal to 1
//and less than or equal to 1000

5 <= A[K] <= 100  //means the value of each member of Array A is greater than or equal to 5
//and less than or equal to 100
``````

Code-chef is very serious ,they wont give u the data that is not allowed or that does not satisfy the constraint.Code-chef infact guarantee that there is no test data that does not satisfy the given constraint.

" if user try to input data that is not allowed ask him again for data or it is just to let us see the limits of input"

Well it never gives such data .You don’t have to unnecessarily check and skip every-time while Taking Input.

4 Likes

Constraints are, as you said, the LIMITS (Upper and Lower) of the input data, and are VERY important when considering the solution of a problem. They give an idea of the data types to be used, approach to be adopted etc.

Eg-

Lets say input constraint of an integer n is 10^15. (10 raised to power 15)

This means that-

1)Using int to store the data will result in overflow (Here it hinted on data type to be used)

1. If you think of creating an array of this size, you’d exceed memory limit of code and hence arrays cannot be used (here it ruled out any approach using arrays)

2. With n this large (if its something we have to iterate over), a nested loop or anything using slower algo like O(n^2) will usually give TLE (exceed time limit. Since operations to be performed are a lot)

And many more! You’d get the idea as you progress and practie!

Hope it helped, happy coding! 9 Likes

Correct, here is link to FAQ - http://www.codechef.com/wiki/faq#What_should_I_do_with_invalid_input: “There will not be invalid input.”

Thanks man, yours is the best and to the point answer with logical example.

1 Like

Hi, is it the same on most online judges? It will not give any input which is not within the constraints? And we don’t have to handle the cases where input is not within the constraints right? @ritesh_gupta @vijju123

For most serious online judges (like Codeforces), their official problems should definitely have good input.

7 Likes

XD even O(n) will not pass.

2 Likes

1 <= T but for some reason, I got the wrong answer because I didn’t test for 0. sometimes their constraints can be misleading