Middle of the Linked List
Real-world version: Pump Chain MidpointThe statement here is our own: the same inputs, outputs and technique.
A booster pipeline is a chain of pump stations. Each station knows only the pressure it runs at and which station comes next, so the chain can be travelled in one direction and its length is not recorded anywhere.
For this exercise the chain is handed to you as pressures, a flat list of the pressures in travel order, where index 0 is the station at the intake. Your answer is the same kind of flat list: the pressure of the midpoint station followed by the pressures of every station downstream of it, in travel order. That is the JSON stand-in for handing back a station and letting the rest of the chain trail behind it.
When the chain holds an even number of stations, two of them sit equally near the centre; pick the one further downstream.
Travel the chain a single time. Do not walk it once to total up the stations and then walk it again to reach the answer.
Examples
Example 1
- Input
- pressures = [42, 17, 63, 8, 55]
- Output
- [63, 8, 55]
Five stations put the centre at index 2, and the two stations downstream of it trail along behind.
Example 2
- Input
- pressures = [12, 30, 7, 91, 44, 6]
- Output
- [91, 44, 6]
Six stations leave indices 2 and 3 tied for the centre, and the tie goes to the one further downstream.
Example 3
- Input
- pressures = [100]
- Output
- [100]
A lone station is its own midpoint and has nothing trailing it.
Constraints
- 1 <= pressures.length <= 100
- 1 <= pressures[i] <= 100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def midpoint_segment(pressures: list[int]) -> list[int]:public int[] midpointSegment(int[] pressures)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's