Open In App

Find time required to reach point N from point 0 according to given rules

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Examples:

Input: N = 5, X = 4, Y = 3
Output : 28
Explanation:
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
Output: 54

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:
    1. Initialize the variable check as currtime/Y.
    2. If check%2 is equal to 0, then add the value of x to the variable currtime.
    3. 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:

C++




// C++ program for the above approach
 
#include <iostream>
using namespace std;
 
// Function to get the total time to
// reach the point N
int getcurrtime(int N, int X, int Y)
{
    // Store the answer
    int currtime = 0;
 
    // Iterate over the range
    for (int i = 0; i < N; i++) {
 
        int check = currtime / Y;
 
        // If barrier is open
        if (check % 2 == 0)
 
            // Travel from that point
            // to the next one
            currtime += X;
        else {
 
            // Wait until it becomes the
            // next greater integer of Y
            currtime = (currtime + Y) / Y;
            currtime = currtime * Y;
 
            // Time to reach n point
            int repeat = (N - 1) / i;
            currtime = currtime * repeat
                       + (N - (repeat * i)) * X;
            break;
        }
    }
    return currtime;
}
 
// Driver Code
int main()
{
 
    int N = 7;
    int X = 6;
    int Y = 2;
 
    cout << getcurrtime(N, X, Y);
 
    return 0;
}


Java




// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to get the total time to
    // reach the point N
    static int getcurrtime(int N, int X, int Y)
    {
        // Store the answer
        int currtime = 0;
 
        // Iterate over the range
        for (int i = 0; i < N; i++) {
 
            int check = currtime / Y;
 
            // If barrier is open
            if (check % 2 == 0)
 
                // Travel from that point
                // to the next one
                currtime += X;
            else {
 
                // Wait until it becomes the
                // next greater integer of Y
                currtime = (currtime + Y) / Y;
                currtime = currtime * Y;
 
                // Time to reach n point
                int repeat = (N - 1) / i;
                currtime = currtime * repeat
                           + (N - (repeat * i)) * X;
                break;
            }
        }
        return currtime;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 7;
        int X = 6;
        int Y = 2;
 
        System.out.println(getcurrtime(N, X, Y));
    }
}
 
// This code is contributed by Dharanendra L V.


Python3




# Python 3 program for the above approach
 
# Function to get the total time to
# reach the point N
def getcurrtime(N, X, Y):
    # Store the answer
    currtime = 0
 
    # Iterate over the range
    for i in range(N):
        check = currtime / Y
 
        # If barrier is open
        if (check % 2 == 0):
 
            # Travel from that point
            # to the next one
            currtime += X
        else:
 
            # Wait until it becomes the
            # next greater integer of Y
            currtime = (currtime + Y) / Y
            currtime = currtime * Y
 
            # Time to reach n point
            repeat = (N - 1) / i
            currtime = currtime * repeat + (N - (repeat * i)) * X
            break
    return int(currtime)
 
# Driver Code
if __name__ == '__main__':
    N = 7
    X = 6
    Y = 2
 
    print(getcurrtime(N, X, Y))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#




// C# program for the above approach
using System;
 
class GFG
{
 
    // Function to get the total time to
    // reach the point N
    static int getcurrtime(int N, int X, int Y)
    {
       
        // Store the answer
        int currtime = 0;
 
        // Iterate over the range
        for (int i = 0; i < N; i++) {
 
            int check = currtime / Y;
 
            // If barrier is open
            if (check % 2 == 0)
 
                // Travel from that point
                // to the next one
                currtime += X;
            else {
 
                // Wait until it becomes the
                // next greater integer of Y
                currtime = (currtime + Y) / Y;
                currtime = currtime * Y;
 
                // Time to reach n point
                int repeat = (N - 1) / i;
                currtime = currtime * repeat
                           + (N - (repeat * i)) * X;
                break;
            }
        }
        return currtime;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int N = 7;
        int X = 6;
        int Y = 2;
 
        Console.WriteLine(getcurrtime(N, X, Y));
    }
}
 
// This code is contributed by ukasp.


Javascript




<script>
        // JavaScript Program to implement
        // the above approach
 
        // Function to get the total time to
        // reach the point N
        function getcurrtime(N, X, Y)
        {
         
            // Store the answer
            let currtime = 0;
 
            // Iterate over the range
            for (let i = 0; i < N; i++) {
 
                let check = Math.floor(currtime / Y);
 
                // If barrier is open
                if (check % 2 == 0)
 
                    // Travel from that point
                    // to the next one
                    currtime += X;
                else {
 
                    // Wait until it becomes the
                    // next greater integer of Y
                    currtime = Math.floor((currtime + Y) / Y);
                    currtime = currtime * Y;
 
                    // Time to reach n point
                    let repeat = Math.floor((N - 1) / i);
                    currtime = currtime * repeat
                        + (N - (repeat * i)) * X;
                    break;
                }
            }
            return currtime;
        }
 
        // Driver Code
        let N = 7;
        let X = 6;
        let Y = 2;
 
        document.write(getcurrtime(N, X, Y));
 
     // This code is contributed by Potta Lokesh
    </script>


Output: 

54

 

Time Complexity: O(N)
Auxiliary Space: O(1)



Last Updated : 24 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads