Convert Speed

Easy
0/40
0 upvote
Asked in company
Tata Consultancy Services (TCS)

Problem statement

You are given 'X', representing the speed of an object in kmph.

Convert the speed to m/sec. Your result should be accurate up to 3 decimal places.

Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line of input contains a single integer 'X', which denotes the speed of the object in kmph.
Output Format :
Return the speed of the object in m/sec. 
Note :
You don’t need to print anything. Just implement the given function.
Constraints :
10 <= 'X' <= 10^9

Time Limit: 1 sec
Sample Input 1 :
18
Sample Output 1 :
5
Sample Input 2 :
120
Sample Output 2 :
33.33
Hint

Speed (m/sec) = Speed(kmph) * 5 / 18

Approaches (1)
Maths

Approach:

  • Let us determine the formula for the conversion
    • We know that: 1 km = 1000 metre & 1 hour = 60 minutes = 3600 seconds
    • Therefore, 1 kmph = 1000 metre / 3600 seconds = ( 5 / 18 ) m/sec
    • Thus, we need to multiply the input with ( 5 / 18 ) to get our answer.
  • We need to avoid integer overflow and floating point precision errors while using the above formula.


 

Algorithm:

  • Return ‘( 5 * X ) / 18’
     
Time Complexity

We only perform a mathematical operation.
Thus, the overall time complexity is of the order O(1).

Space Complexity

We do not use any extra space.
Thus, the overall space complexity is of the order O(1).

Code Solution
(100% EXP penalty)
Convert Speed
Full screen
Console