Find time required to reach point N from point 0 according to given rules
Given two positive integers X and Y and N number of points arranged in a line, the task is to find the time required to reach point N from point 0 according to the following rules:
- Every point has one barrier that closes after every Y minutes and remains closed for the next Y minutes.
- On reaching a point if the barrier is closed, then the person needs to wait until it opens.
- The time taken to move from one point to another is X minutes.
- Initially, all barriers are opened.
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: N = 5, X = 4, Y = 3
Output : 28
It takes 4 units of time to reach point number 1 from point number 0 and an additional wait for 2 minutes(till 4th unit of time the barrier was closed on 3 unit time and has to wait for 6th unit time to reopen). Therefore the total time for current hop is 6 unit.
Similarly, a total of 24 units of time to reach point 4 and wait there, and finally 4 units of time to reach point 28.
Input: N = 7, X = 6, Y = 2
Approach: The given problem can be solved based on the following observation:
- If the time for reaching the first point i.e., N=1 is found or when the first barrier will be there, then it will become easy to find the answer.
- Keep track of the current time when reaching each point. If, on reaching a city, the barrier is open, then move to the next point, else wait till the barrier gets open.
- To check if the barrier is open, check if the current time lies in the time period when the barriers are open or closed.
- Divide the current time by Y and if on dividing the time, the current comes to be even, the barrier is open, else they are closed. If the current time is odd, wait till the current time becomes the next greater multiple of Y and then move ahead.
- Observe that if X<Y, then every time it reaches a point, wait for the barrier to open and then start and the process repeats so the answer will be (N – 1)*Y + X. Similarly, for Y>=X, when the first barrier is encountered, calculate the time needed to reach that point and use it to calculate our total time to reach N.
Follow the steps below to solve the problem:
- Initialize the variable, say currtime as 0 that stores the total time required.
- Iterate over the range [0, N] using the variable i and perform the following steps:
- Initialize the variable check as currtime/Y.
- If check%2 is equal to 0, then add the value of x to the variable currtime.
- Otherwise, increase the value of currtime by (currtime+Y)/Y and multiply currtime by Y and add the value of currtime * repeat + (N – (repeat * i)) * X to the variable currtime and break.
- After performing the above steps, print the value of currtime as the answer.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)