Wednesday, October 8, 2025
HomeData Modelling & AITotal money to be paid after traveling the given number of hours

Total money to be paid after traveling the given number of hours

Given the travel cost of a cab is m rupees for first n hours per hour and then is x rupees per hour, given the total time of travel in a minute the task is to find the total cost.
Note: If an hour starts then the traveler has to give the full cost for that hour.
Examples: 

Input:  m = 50, n = 5, x = 67, mins 2927
Output: 3198
m = 50, n = 5, x = 67, Minutes travelled = 2927, hours travelled = 49, cost = 5 * 50 + (49-5) * 67 = 2927
Input: m = 50, n = 40, x = 3, mins = 35
Output: 50

Approach: First calculate the hours traveled by taking the ceiling of (minutes traveled)/60 as if we start an hour we have to pay the price of a full hour. Then we see that our hours calculated is less than equal to n or not if it is then we directly give the answer as m*hours traveled else answer is n * m + (hours_calculated – n) * x.

Below is the implementation of the above approach: 

C++




// CPP implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
int main(){
 
    float m=50, n=5, x=67, h=2927;
 
    // calculating hours travelled
    int z = (ceil(h / 60*1.0));
    if(z <= n)
        cout<<z * m;
    else
        cout<<n * m+(z-n)*x;
 
    return 0;
}
 
// This code is contributed by Sanjit_Prasad


Java




// Java implementation of the above approach
 
import java.io.*;
 
class GFG {
     
 
    public static void main (String[] args) {
            float m=50, n=5, x=67, h=2927;
 
    // calculating hours travelled
    int z = (int)(Math.ceil(h / 60*1.0));
    if(z <= n)
        System.out.println(z * m);
    else
        System.out.println(n * m+(z-n)*x);
    }
}
 
// This code is contributed by shs


Python3




# Python3 implementation of the above approach
 
import math as ma
m, n, x, h = 50, 5, 67, 2927
 
# calculating hours travelled
z = int(ma.ceil(h / 60))
if(z <= n):
    print(z * m)
else:
    print(n * m+(z-n)*x)


C#




// C# implementation of the above approach
 
using System;
 
class GFG {
     
 
    public static void Main () {
            float m=50, n=5, x=67, h=2927;
 
    // calculating hours travelled
    int z = (int)(Math.Ceiling((h / 60*1.0)));
    if(z <= n)
      Console.WriteLine(z * m);
    else
        Console.WriteLine(n * m+(z-n)*x);
    }
}
 
// This code is contributed by anuj_67..


PHP




<?php
// PHP implementation of the
// above approach
$m = 50;
$n = 5;
$x = 67;
$h = 2927;
 
# calculating hours travelled
$z = (int)(ceil($h / 60));
if($z <= $n)
    print($z * $m);
else
    print($n * $m + ($z - $n) * $x);
 
// This code is contributed by mits
?>


Javascript




<script>
  
// JavaScript implementation of the above approach
var m=50, n=5, x=67, h=2927;
 
// calculating hours travelled
var z = (Math.ceil(h / 60*1.0));
if(z <= n)
    document.write( z * m);
else
    document.write( n * m+(z-n)*x);
 
</script>


Output

3198

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32341 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6709 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6832 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6783 POSTS0 COMMENTS
Umr Jansen
6786 POSTS0 COMMENTS