- 0
Notifications Mark All Read
No New notification
- Login
- Get Prime
TCS DRC Coding Question-1
Problem Statement –
A marathon is a long-distance race with an official distance of 42.195 kilometers(26 miles 385 yards), usually run as a road race or footrace. A local marathon was organized at Bavdhan, Pune. The distance actually covered by the participants has been recorded in an array R[ ] which is an integer array holding the values in kilometers. If there are N number of participants who started running at a particular time, then the size of R is N. The participants should cover a distance more than 0.0 km to get recorded in array R[ ].
Find the maximum distances covered by the 3 highest racers excluding finishers. If there are only one or two racers excluding finishers, give their distances covered.
R[ ] will be the input float array. Write code to take the Input array R[ ], and return 3 maximum distances excluding Finishing Distance d, d = 42.195 km
Example-1
Input Values
Enter the distances covered by racers in Marathon(Kilometers) please
(press q to terminate):
42.195
42.195
42.195
33.25
40
41.2
38.9
37.5
q
Output Values
Highest Distances excluding Finishers:
[41.2, 40.0, 38.9]
Solution-
R = [] print("Enter the distances covered by racers in Marathon(Kilometers) please (press q to terminate):") num = input() while num !='q': R.append(float(num)) num = input() result = sorted(R) for i in result: if i < 0 : print("Invalid Input") break print(result[-4:-7:-1])
import java.util.Scanner;
public class Maratho {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the no of participants”);
int n=sc.nextInt();
Float d= (float) 42.95;
Float temp;
Float R[]=new Float[n];
for(int i=0;i<n;i++)
{
R[i]=sc.nextFloat();
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(R[j]<R[j+1] && R[j]!=42.95)
{
temp=R[j];
R[j]=R[j+1];
R[j+1]=temp;
}
}
}
System.out.println(R[0]);
System.out.println(R[1]);
System.out.println(R[2]);
}
}