In this problem you should answer to 4 questions:
1) Can we use type byte to store N?
2) Can we use type short to store N?
3) Can we use type int to store N?
4) Can we use type long to store N?
We should check these conditions in the given order. If all these conditions are wrong, the answer is BigInteger.
The simplest way to check these conditions is to store numbers as strings and write a function to compare such strings. In Java you can use type BigInteger.
Try to check all possibilities for creation artificial rain and calculate how many sections contain water. The maximal answer from all these possibilities is the answer for the problem. To calculate the answer for the given position we should check how many sections are to the left and to the right of the given section receive water. The complexity of this algorithm is O(N^2).
Will be published later.
Consider N distinct prime numbers: p1, p2, …, pn.
Let A=p1*p2*…*pn. Then, easy to see, that the numbers A/p1, A/p2, …, A/pn can be considered as the answer.
The special case is when N=2. In this case there is no answer. We can see that this solution needs long arithmetic. If we choose first n prime numbers as p1, p2, …, pn then the maximal number in the answer for all N<=50 contains less than 100 digits.
Of course, there are other solutions. For example, if N=3 numbers 15, 10, 6 are the answer, and for N>3 numbers 15, 10, 6, 6*2, 6*3, …, 6*(N-2) are the answer.
First of all we divide our problem into 2 parts: consider stations from which we can start if we are moving in the clockwise direction and stations from which we can start if we are moving in the counterclockwise direction.
Obviously, if we know the solution of one of these problems, we know the solution of another problem.
So, we may assume that stations are located in the counterclockwise order and we are moving in the counterclockwise direction.
Consider the following differences:
Obviously if one of Di’s is less than a zero, then we cannot drive one round along the road. Let D = min(Di) – we will use it later.
Obviously, if D<0 then the first station cannot be the start station.
Now, we can check with complexity O(n) whether the first station can be used as the starting point. Next, we want to show how we can check this for the second station with complexity O(1).
To show this, consider:
Next, substitute Di in these equalities. We get the following:
E1=(a1-b1)-(a1-b1)=0=(a2+a3+…+an+a1)-(b2+b3+…+bn+b1) – (a1+…+an=b1+…+bn=X)
But it’s easy to see that number E1 has the same meaning for the second station as number D1 for the first one. So, we just have to check min(Ei)>=0. But Ei=Di-(a1-b1), so we have to check min(Di-(a1-b1))>=0. Now, we can see that if min(Di)=Dk, then min(Di-(a1-b1))=Dk-(a1-b1). So, if we know Dk, that we can check whether the second station can be the starting point with complexity O(1). Similarly, we can check this for the third, the fourth, …, the nth stations.
Now we should check the same things but assuming that the car is moving in the clockwise direction.