A perimeter security fence carries a line of solar beacons wired in a single forward-only daisy chain: each beacon knows only the reading of the next beacon down the line, never the one before it. A maintenance drone docks directly onto exactly one beacon at a time and is told which position in the line that is; it is guaranteed the drone never docks on the final beacon in the chain. Because the drone cannot reach backwards, it repairs the chain using only forward information: it overwrites its docked beacon's stored reading with the next beacon's reading, then relinks its beacon directly to the beacon after that one, skipping the beacon it just copied from and removing it from the chain entirely.
Given the full sequence of beacon readings and the 0-indexed position where the drone docks, report the sequence of beacon readings that remains once the splice completes.
Line 1: an integer n, the number of beacons.
Line 2: n space-separated integers, the beacon readings in chain order.
Line 3: an integer p, the 0-indexed position where the drone docks.
Print the n - 1 remaining beacon readings, in chain order, space-separated on a single line.
2 <= n <= 10^5-10^9 <= reading <= 10^9 for every beacon reading (readings need not be distinct)0 <= p <= n - 2 (the drone never docks on the last beacon)Example 1
Input
5 4 9 1 7 2 2
Expected
4 9 7 2
Explanation
The drone docks at position 2 (reading 1), which is not the last beacon. It copies the next beacon's reading (7) into itself, then relinks past that beacon to the one after it (reading 2). The beacon that held 7 is removed, leaving readings 4 9 7 2.
Example 2
Input
2 10 -5 0
Expected
-5
Explanation
With only two beacons, position 0 is the only legal docking spot (position 1 would be the last beacon). The drone copies the last beacon's reading (-5) into itself and unlinks the last beacon, leaving a single beacon reading -5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →