Newton School - Question of the Day - Meet in Time - 16 March 2023

 

            

  


Meet in Time:-

Problem Statement

Alexa is meeting up with Bob. They have planned to meet at a place that is D meters away from Alexa's house in T minutes from now. Alexa will leave her house now and go straight to the place at a speed of
S meters per minute. Will she arrive in time?

Input
The input consists of three space separated integers.
D T S


Constraints
1 ≤ D ≤ 10000
1 ≤ T ≤ 10000
1 ≤ S ≤10000
All values in input are integers.

Output
If Alexa will reach the place in time, print Yes; otherwise, print No.

Approach:

In this problem we have given three integers that is D, T and S. D is for Distance, T for Time and S for Speed. We have to check that if alexa can reach to the distance D within given time or Not. If she can reach to the distance within time then we have to print Yes else we have to print No, that means she can not reach to the distance within time.

To solve this problem we are going to use the simple Distance calculation math formula.

            Distance = Speed x Time

By using above formula we can find the total distance Alexa can reach with given time and speed. let's take one example D=200, T=10 and S=50 so with this we can find how much the total distance she can reach with given speed and time.

So distance = speed * time = 50 * 10 = 500 so with given speed and time she can reach the distance 500 meters away from her home.

Now let's try to understand how we can check that if alexa can reach to the given distance within time or Not for that first we find total distance alexa can reach as explained above using distance formula.

So, we have to check that if given distance is greater or less than speed * time. if the given distance is less than speed * time then we can say alexa can reach to the D meters distance within given time and else if the given distance is greater than speed * time then she can not reach.

Let's take one example to understand this.

D= 200 T=10 and S=50

So first will find total distance using speed * time = 50 * 10 = 500.

then, we can check if the given distance is less or greater than equal to the speed * time.
200 is less than 500, So in 10 minutes alexa can go 500 maximum meters away from her home so in this case we can say alexa can reach to the given distance within time. So we have to print Yes.

let's say if D=200, T=2 and speed =50, in this case the distance is greater than speed *time so we have to print No.

            ans = speed * time = 50 * 2 = 100
            
                    so distance 200 is greater than 100 so we have to print - No.


Now lets try to understand how we can find how much exact minutes alexa take to reach the given distance. For that 

First we need to find the total distance =speed * time and then we have to divide it with 100 so that we get the exact time.

So, lets take above example only D=200, T=10 and S=50

                Formula = (Speed * Time / 100)

                                = 50 * 10 / 100
                                = 500/100
                                = 5

So in above example alexa need exact 5 to 5:30 minutes to react the given distance. This is the approach to solve the problem. 

Now lets see the solution.

Solution:-

Java Code:-

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        // Your code here
        Scanner sc=new Scanner(System.in);
        int distance = sc.nextInt();
        int time = sc.nextInt();
        int speed = sc.nextInt();

        int result = speed * time;

        if(distance <= result){
            System.out.print("Yes");
        }
        else {
            System.out.print("No");
        }
    }
}

C++ Code:-

#include <bits/stdc++.h> // header file includes every Standard library
using namespace std;

int main() {
    int D, T, S;
    scanf("%d %d %d", &D, &T, &S);
 
    if((S*T)>=D)
        printf("Yes");
    else
        printf("No");
}

JavaScript Code:-

// Your code here

const meet = arg => {
    const [d,t,s,] = arg.trim().split(" ").map((x)=>parseInt(x));
    if(d<= s * t){
        console.log("Yes");
    }else{
        console.log("No")
    }
}
meet(require('fs').readFileSync('/dev/stdin', 'utf8'));

Python Code:-

# Your code here

d,t,s = map(int,input().split())
if s * t >= d:
  print("Yes")
else:
  print("No")

In this article i have provided the approach and solution for this problem. I tried to provide the solution in C++, Java, JavaScript and Python. Java Solution is working correctly but in other languages solution may be you need to do some changes or may not be.

If you like this article then do like it and share it with your friends.

Thanks,
TechKnow Learning   

No comments:

Powered by Blogger.