GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Sunday 14 August 2022

[Solution] Two Trains CodeChef Solution | Solution CodeChef


Problem

There are 2 trains A and B and N stations in a line from 1 to N in order. There is also an array P of length N-1 such that P_i (1\le i \lt N) denotes the amount of time any train takes to go from the i-th station to the (i+1)-th station.

Initially, both trains are at station 1. Train A departs from station 1 and stops directly at station N. For safety purposes, it is maintained that train B cannot depart from station i unless train A has already reached station (i+1) (1 \le i \lt N).

Find the minimum time after which train B reaches station N, once train A departs from station 1.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of two lines of input.
    • The first line of each test case contains an integer N, denoting number of stations.
    • The next line contains N-1 space-separated integers, P_1,P_2,\ldots ,P_{N-1}.

Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇

Output Format

For each test case, output on a new line the minimum time after which train B reaches station N.

Explanation:

Test case 1: A reaches station 2 at t=4 and at this time B departs from station 1 and reaches station 2 at t=4+4=8.

Test case 1: Following is the timeline of two trains-

  • At t=3A reaches station 2 and B departs from station 1.
  • At t=6B reaches station 2 but A has not yet reached station 3, so train B will wait at station 2.
  • At t=8A reaches station 3 and B departs from station 2.
  • At t=8+5=13, train B reaches station 3.

No comments:

Post a Comment