All problems
0195EasyLinked ListTwo Pointers

Pump Chain Midpoint

Tracked in this browser only
Write code

Trains the technique from

LeetCode 876Middle of the Linked List

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def midpoint_segment(pressures: list[int]) -> list[int]:
Java
public int[] midpointSegment(int[] pressures)
September 7
Apply