Wednesday, November 29, 2017

CodeForces: Xenia and Ringroad(339B) Solution in Java for Beginners

  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 ->

  1. Loops from geeksforgeeks
  2. Conditional operators in Java from w3resource 
.
.
.
.
.

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.

Interview Questions at Enosis(Part 3)

In Part 2 , I have discussed 3 coding problems out of 6. Here we will talk about the next 3 coding problems. Problem 4: Write a function...