PracticeArrays
Neighboring Dock Totals
Context
A warehouse records signed crate-count adjustments at successive loading checkpoints along a delivery route. For planning handoffs, the dispatcher needs the combined adjustment for each pair of neighboring checkpoints, preserving route order.
Problem
Write a function that receives the ordered checkpoint adjustments for one delivery route. For every consecutive pair of values, calculate their sum and place it in the result in the same order as the pairs appear. If the route has fewer than two checkpoints, return an empty array. Process each valid neighboring pair exactly once and never access a position beyond the route.
Examples
Example 1
Input:
adjustments = [4, 7, 2]Output:
[11, 9]Explanation: The adjacent sums for [4, 7, 2] are 4 + 7 = 11 and 7 + 2 = 9, producing [11, 9] in pair order.
Example 2
Input:
adjustments = [-5, 3, -2, 8]Output:
[-2, 1, 6]Explanation: The adjacent sums for [-5, 3, -2, 8] are -5 + 3 = -2, 3 + -2 = 1, and -2 + 8 = 6, producing [-2, 1, 6].
Example 3
Input:
adjustments = [12]Output:
[]Explanation: The input [12] has fewer than two adjustments, so the executed result is an empty array.
Constraints
0 <= len(adjustments) <= 100000-1000000000 <= adjustments[i] <= 1000000000 for every i- Types:
adjustmentsis int[]; result is int[]
Function signature
def neighboring_dock_totals(adjustments: list[int]) -> list[int]
adjustmentslist[int]- Signed crate-count adjustments recorded at consecutive route checkpoints.
- returns list[int]
- An array containing the sum of each consecutive pair of adjustments, ordered from the first pair to the last.
Adapted from MBPP problem task_503 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The input may be empty or contain one value.
- Repeated and zero values remain separate entries and participate normally.
- The result contains one value for each valid neighboring pair.
Code
Saved