Xenia and Ringroad(339B) Problem Link:
Since this is a beginner's problem, I suggest you try it out yourself first.
Java programming topics you need to have knowledge of ->
Since this is a beginner's problem, I suggest you try it out yourself first.
Java programming topics you need to have knowledge of ->
.
.
.
.
.
Explanation of Solution:
After taking the necessary input in an array (the house numbers), we assign the variable result (the total time) a[0] - 1: this is the time to go to the 1st house (from house 1 to that house).
We enter a for loop now to traverse all the houses/array elements one by one. In each iteration, if the current element/house number under consideration is greater than the element before, it is in front of the previous house. In this case, we just add the difference of the house numbers.
In the 'else' case, we see the house number is less than the previous one meaning we go to that house after completing the rotation. So we apply the rotational equation n - (a[i-1] - a[i]).
Accumulating the results like this, we get our final solution.