Time taken per hour for stoppage of Car
Last Updated :
04 Jun, 2022
A car travels with an average speed of S km/h without any stoppage and with stoppage the speed of car reduces to an average of S1 km/h. The task is to find the time wasted per hour for the stoppage.
Examples:
Input: S = 50, S1 = 30
Output: 24 min
Input: S = 30, S1 = 10
Output: 40 min
Approach: Take the first example,
Speed of car without any stoppage = 50 kmph i.e. 50 km in 60 min.
Speed of car with stoppage = 30 kmph i.e. 30 km in 60 min.
Now, if there will be no stoppage then 30 km can be covered in 36 min.
50 km –> 60 min
30 km –> (60 / 50) * 30 = 36 min
but it takes 60 min.
So, time for stoppage per hour is 60 min – 36 min = 24 min.
This can be calculated using the below formula:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfMinutes( int S, int S1)
{
int Min = 0;
Min = ((S - S1) / floor (S)) * 60;
return Min;
}
int main()
{
int S = 30, S1 = 10;
cout << numberOfMinutes(S, S1) << " min" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int numberOfMinutes( int S, int S1)
{
int Min = 0 ;
Min = ( int ) (((S - S1) / Math.floor(S)) * 60 );
return Min;
}
public static void main(String[] args)
{
int S = 30 , S1 = 10 ;
System.out.println(numberOfMinutes(S, S1) + " min" );
}
}
|
Python3
import math
def numberOfMinutes(S, S1):
Min = 0 ;
Min = ((S - S1) / math.floor(S)) * 60 ;
return int ( Min );
if __name__ = = '__main__' :
S, S1 = 30 , 10 ;
print (numberOfMinutes(S, S1), "min" );
|
C#
using System;
class GFG
{
static int numberOfMinutes( int S, int S1)
{
int Min = 0;
Min = ( int ) (((S - S1) /
Math.Floor(( double )S)) * 60);
return Min;
}
public static void Main()
{
int S = 30, S1 = 10;
Console.WriteLine(numberOfMinutes(S, S1) +
" min" );
}
}
|
Javascript
<script>
function numberOfMinutes(S, S1)
{
let Min = 0;
Min = ((S - S1) / Math.floor(S)) * 60;
return Min;
}
let S = 30, S1 = 10;
document.write(numberOfMinutes(S, S1) + " min" );
</script>
|
Time Complexity: O(1), since there is only basic arithmetic that happens in constant time.
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...