Reorder Shipment Loads
Context
A warehouse records the package counts loaded onto vehicles in departure order. Dispatch changes the schedule by moving the final portion of this list to the front, wrapping around when the requested displacement exceeds the list length. Build the adjusted departure sequence without changing any recorded count.
Problem
Given a list of integer shipment loads and a requested rightward displacement, return the list after cyclically moving elements to the right. A positive displacement moves the final elements to the beginning; a negative displacement moves elements in the opposite direction. Displacements may be zero, larger than the list length, or negative. Normalize the displacement using the list length, then split the list at the boundary and place the trailing segment before the leading segment. Preserve the list length, every occurrence, and the internal order of both segments. An empty list must return empty, and a one-element list must remain unchanged.
Examples
displacement = 2loads = [12, 7, 19, 4][19, 4, 12, 7]displacement = -2loads = [5, 11, 3, 8, 6][3, 8, 6, 5, 11]displacement = 8loads = [9, 1, 6][1, 6, 9]Constraints
0 <= len(loads) <= 100000-1000000000 <= loads[i] <= 1000000000 for every i-1000000000 <= displacement <= 1000000000- Types:
loadsis int[],displacementis int; result is int[]
Function signature
def reorder_shipment_loads(loads: list[int], displacement: int) -> list[int]
loadslist[int]- Shipment counts in their current departure order.
displacementint- Requested cyclic rightward displacement, which may be negative or larger than the list length.
- returns list[int]
- The shipment counts in their cyclically displaced departure order, with all original elements preserved.
Adapted from MBPP problem task_743 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The returned list must be newly formed or otherwise provide the exact rearranged sequence.
- A zero-length input must not require indexing.