What is use of constraint in problem example

Constraint:

1<=testcases<=10 etc. Do we have to write a condition statement to check this constraint in our code?

There is no need to check this constraintâ€¦What it means is that there will be a maximum of 10 test cases in each test file against which your code is checked by the online judgeâ€¦

The constraints whatever they be , provide the base for any coder to write a programme .ieâ€¦whether the algorithm which you are following to solve a problem is suitable for the given constraints or not .

Let me explain this with the help of a few examples â†’

1<=N<=1000

Suppose for the above constraints we have to solve a problem , say sort a list of given noâ€™s :

So here even if you use an O(n^2) algorithm for sorting say bubble sort , it would work without an problem . The reason being that a computing machine can do upto 10^6/10^7 computations per second .

1<=N<=100000

Now to sort a list with 100000 elements ieâ€¦ 10^10 computations need to be done for the above n^2 algorithm would obviously time out , thus we use qsort/mergesort O(nlogn) .

Now suppose a problem has very high constraints say :

1<=N<=10^18

So in such a case your mind should come up with solutions with O(logn) complexity , because even O(n) would time out for the reasons mentioned above .

Some Extra things to be kept in mind :

Suppose in a problem , if you think that solution for a problem is O(n^2) / any other polynomial complexity , then here Dynamic programming can help you to optimise the solution .

Crux of The discussion : So what we can conclude is that the constraints always help you to plan which data structure / algorithm is to be followed to solve a computing problem .

Some useful links :

If in case you are not familiar with the big(0) notation , this link will help you get started :â€“> Code n Learn: Understanding Algorithm complexity, Asymptotic and Big-O notation

In case of any doubt regarding learning new data structures / dp / anything , the below is the go to page :---->

http://help.topcoder.com/data-science/competing-in-algorithm-challenges/algorithm-tutorials/

And yes I hope the above helps you . Happy coding

By seeing the constraints, one can get the worst time algorithm.Also,it helps to come up with a better algorithm which would work under given constraints.

Nice Question , the answer to it has cleared my doubtâ€™s regarding , how to use them and come up to an answer .

Thanks

He has waited all these years for you, finally, you came with your mystic *â€śThanksâ€ť*.

Haha! Atleast better than most posts that have **literally** good answers and even the one who asks the question doesnâ€™t bother to like or say *thanks*.

@its_pheonix thanks for such elaborated explanation

i was afraid of this constraint term and always try to put that in code but your answer has not just made to avoid the mistake but nicely cleared exact use to it.

great answer bro

thanks