PracticeArrays

Reorder Shipment Loads

EasyArraystraversalindex arithmeticmodular arithmetic Time not estimated Not started

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

Example 1
Input: displacement = 2loads = [12, 7, 19, 4]
Output: [19, 4, 12, 7]
Explanation: For [12, 7, 19, 4] with displacement 2, the normalized shift is 2 and the boundary is 2; the suffix [19, 4] is placed before the prefix [12, 7], producing [19, 4, 12, 7]. (,, )
Example 2
Input: displacement = -2loads = [5, 11, 3, 8, 6]
Output: [3, 8, 6, 5, 11]
Explanation: For [5, 11, 3, 8, 6] with displacement -2, the normalized shift is 3 and the boundary is 2; the suffix [3, 8, 6] is placed before the prefix [5, 11], producing [3, 8, 6, 5, 11]. (,, )
Example 3
Input: displacement = 8loads = [9, 1, 6]
Output: [1, 6, 9]
Explanation: For [9, 1, 6] with displacement 8, the normalized shift is 2 and the boundary is 1; the suffix [1, 6] is placed before the prefix [9], producing [1, 6, 9]. (,, )

Constraints

  • 0 <= len(loads) <= 100000
  • -1000000000 <= loads[i] <= 1000000000 for every i
  • -1000000000 <= displacement <= 1000000000
  • Types: loads is int[], displacement is int; result is int[]

Function signature

def reorder_shipment_loads(loads: list[int], displacement: int) -> list[int]
loads list[int]
Shipment counts in their current departure order.
displacement int
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.
Approved · cc031-abs_143_v114 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1