Open In App

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:

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:

Follow the steps below to solve the problem:

Below is the implementation of the above approach:




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




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




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


Article Tags :