Open In App

Program to find the head start in a race

Last Updated : 31 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given the head start that A gives to B and C in a 100-meters race. The task is to find the head-start that B can give to C in the same race.

Examples: 

Input: B = 10 meters, C = 28 meters
Output: 20 meters
B can give C a start of 20 meters.

Input: B = 20 meters, C = 50 meters
Output: 62 meters
B can give C a start of 62 meters.

Approach:  

Total meters in a race = 100 meters. 
A is ahead of B by 10 meters. When A completed it’s 100 meters B completed it’s 90 meters. 
Similarly, A is ahead of C by 28 meters. When A completed it’s 100 meters C completed it’s 72 meters. 
Now, When B completed it’s 90 meters C completed it’s 72 meters. 
So when B completed it’s 100 meters C completed it’s 80 meters. 
–> (( C * 100) / B) 
–> (( 72 * 100) / 90) i.e 80 meters 
So B can give C a start of 20 meters 
 

Below is the implementation of the above approach: 

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the B start to C
int Race(int B, int C)
{
    int result = 0;
 
    // When B completed it's 100 meter
    // then Completed meters by C is
    result = ((C * 100) / B);
 
    return 100 - result;
}
 
// Driver Code.
int main()
{
    int B = 10, C = 28;
 
    // When A completed it's 100 meter
    // Then completed meters of B and C is
    B = 100 - B;
    C = 100 - C;
 
    cout << Race(B, C) << " meters";
 
return 0;
}


Java




// Java implementation of above approach
public class GFG
{
     
// Function to find the B start to C
static int Race(int B, int C)
{
    int result = 0;
 
    // When B completed it's 100 meter
    // then Completed meters by C is
    result = ((C * 100) / B);
 
    return 100 - result;
}
 
// Driver Code
public static void main(String[] args)
{
    int B = 10;
    int C = 28;
     
    // When A completed it's 100 meter
    // Then completed meters of B and C is
    B = 100 - B;
    C = 100 - C;
     
    System.out.println(Race(B, C) + " meters");
}
}
 
// This code is contributed
// by ChitraNayal


Python3




# Python 3 implementation
# of above approach
 
# Function to find the
# B start to C
def Race(B, C):
 
    result = 0;
 
    # When B completed it's 100 meter
    # then Completed meters by C is
    result = ((C * 100) // B)
 
    return 100 - result
 
# Driver Code
if __name__ == "__main__":
    B = 10
    C = 28
     
    # When A completed it's 100 meter
    # Then completed meters of B and C is
    B = 100 - B;
    C = 100 - C;
     
    print(str(Race(B, C)) + " meters")
 
# This code is contributed
# by ChitraNayal


C#




// C# implementation of above approach
using System;
class GFG
{
 
// Function to find the B start to C
static int Race(int B, int C)
{
    int result = 0;
 
    // When B completed it's 100 meter
    // then Completed meters by C is
    result = ((C * 100) / B);
 
    return 100 - result;
}
 
// Driver Code
public static void Main()
{
    int B = 10;
    int C = 28;
     
    // When A completed it's 100 meter
    // Then completed meters of B and C is
    B = 100 - B;
    C = 100 - C;
     
    Console.Write(Race(B, C) + " meters");
}
}
 
// This code is contributed
// by ChitraNayal


PHP




<?php
// PHP implementation of above approach
 
// Function to find the B start to C
function Race($B, $C)
{
    $result = 0;
 
    // When B completed it's 100 meter
    // then Completed meters by C is
    $result = (($C * 100) / $B);
 
    return 100 - $result;
}
 
// Driver Code
$B = 10;
$C = 28;
 
// When A completed it's 100 meter
// Then completed meters of B and C is
$B = 100 - $B;
$C = 100 - $C;
 
echo Race($B, $C) . " meters";
 
// This code is contributed
// by ChitraNayal
?>


Javascript




<script>
 
// Javascript implementation of above approach
 
// Function to find the B start to C
function Race(B, C)
{
    var result = 0;
     
    // When B completed it's 100 meter
    // then Completed meters by C is
    result = ((C * 100) / B);
 
    return 100 - result;
}
 
// Driver Code
var B = 10, C = 28;
 
// When A completed it's 100 meter
// Then completed meters of B and C is
B = 100 - B;
C = 100 - C;
 
document.write(Race(B, C) + " meters");
 
// This code is contributed by itsok
 
</script>


Output: 

20 meters

 

Time Complexity: O(1)

Auxiliary Space: O(1)



Similar Reads

Number of sequences which has HEAD at alternate positions to the right of the first HEAD
Given that a coin is tossed N times. The task is to find the total number of the sequence of tosses such that after the first head from left, all the alternating positions to the right of it are occupied by the head only. The positions except the alternating position can be occupied by any of head or tail. For example, if you are tossing a coin 10
6 min read
Why start + (end - start)/2 is preferable method for calculating middle of an array over (start + end)/2 ?
I am very sure that everyone is able to find middle index of array once you know start index and end index of array, but there are certain benefits of using start + (end - start)/2 over (start + end)/2, which are described below : The very first way of finding middle index is mid = (start + end)/2But there is a problem with this approach, what if t
8 min read
Find the time when last car reaches end of track in given race
Given a car race track of length N units. where each car moves with a speed of 1 unit per second. Given two arrays left[] and right[] denoting the positions of the cars moving towards left and right respectively. When two cars running in two opposite directions meet at some point on the track, they change their directions and continue moving again
5 min read
Aptitude | Race | Question 2
If Geeta can give a start of 100 m of distance or 20 s of time to her friend Meena in a race of 1000 meters. How much time Geeta will take to cover the 1000 meters? (A) 200 sec (B) 160 sec (C) 180 sec (D) 140 sec Answer: (C) Explanation: That means Meena will cover 100 m in 20 sec =&gt; meena can cover 1000 m in = 200 secs Geeta can give a start of
1 min read
Aptitude | Race | Question 4
If Mohan can give a start of 50 m of distance or 10 s of time to Rohan in a race of 1000 meters. How much time Mohan will take to cover the 1000 meters? (A) 190 sec (B) 200 sec (C) 240 sec (D) 180 sec Answer: (A) Explanation: That means Rohan takes 10 sec to cover 50 meters =&gt; Rohan will cover 1000 meters in 200 sec Mohan is giving a start of 10
1 min read
Aptitude | Race | Question 5
Ram and Sita participate in a race of 1000 meters. If Ram gives Sita a start of 200 meters or 20 seconds. In ow much time Ram completes the race? (A) 70 sec (B) 120 sec (C) 100 sec (D) 80 sec Answer: (D) Explanation: Sita takes 20 sec. to run 200 m. =&gt; Sita takes 100 sec. to run complete 1000 m. Ram gives Sita a start of 20 sec, means Ram is abl
1 min read
Length of race track based on the final distance between participants
Given three integers A, B, and C, the task is to find the length of a race track if 3 racers are competing in a race where the first racer beats the second racer by A meters, the first racer beats the third racer by B meters and the second racer beats the third by C meters. Examples: Input: A = 11, B = 90, C = 80 Output: 880 Input: A = 10, B = 20,
3 min read
Fastest Horse Query in a Race
Given ‘N’ horses running in ‘N’ different lanes numbered from 1 to ‘N’. You are given an array “FINISHTIME” containing ‘N’ integers where “FINISHTIME[i]” represents the time taken by the ith horse to complete the race. Also given are ‘Q’ queries, each containing two integers each, ‘L’ and ‘R’. For each query, return the time taken by the fastest ho
8 min read
Minimum Distance between Runners before the Race Ends
Given two integers N, K and two arrays speed[] and dist[] each of size N, where speed[i] is the speed of the ith runner and dist[i] is the distance which the ith runner is ahead of the starting line, find the minimum value between the most distant runners at any time if the race ends after K seconds. In other words, we have to tell the minimum dist
9 min read
C++ Program For Making Middle Node Head In A Linked List
Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples: Input: 1 2 3 4 5 Output: 3 1 2 4 5 Input: 1 2 3 4 5 6 Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves one at a time and second one moves two at a time.
3 min read