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.

2 comments:

  1. Awesome!! You got the best questions and answers for java interview. You're doing a great job.

    Thanks for sharing.
    http://www.flowerbrackets.com/java-for-loop-example/

    ReplyDelete
  2. You have explained the concept very well. Appreciate your effort.

    Thanks,
    http://www.flowerbrackets.com/while-loop-java/

    ReplyDelete

Beginner's Guide: Some Useful Machine Learning Websites(Part 1)

I have been away for quite some time now. The reason is I was quite busy with my academic thesis and general academic pressure. Now I do fe...