Hash map

Pack a Two-Item Parcel

Original & variantTwo Sum ↗

This version selects the first completed pair and the earliest earlier duplicate.

01. The question

A packing belt carries items with positive integer weights. Find the first pair completed while scanning left to right that exactly fills a parcel. Return their zero-based positions; if several earlier partners fit, use the earliest one. Return [] if no pair fits.

Before revealing the idea, predict the result and describe what information you would need to track.

02. Worked example

Inputweights = [5, 9, 4, 8, 7], capacity = 12

Output[2, 3]

  1. The first weight is 5; it needs a partner of 7. Remember position 0.
  2. Weights 9 and 4 need partners 3 and 8. Neither has appeared yet.
  3. Weight 8 at position 3 needs 4. That is at position 2, so return [2, 3]. Do not wait for the later 5 + 7 pair.

Try your code

Define solve(data, parameter="") and return your result. Your code runs locally, not on a server.

Edit your test cases

Use 1-10 objects with data and an optional string parameter. Expected outputs are computed from the lesson. An optional expected field must be correct.

Input contract & reference code

Example data: [5,9,4,8,7]. Parameter: "12". Numeric parameters arrive as strings; convert them with int(parameter) when needed.

The example and generated cases use the question’s exact variant. This checks behavior on a small test batch—not performance on every possible input.

Python runtime notices

Python downloads only when you run code, using compressed runtime assets. Code is not saved or uploaded. Enable JavaScript to run it.

Five-second execution limit per batch. Standard-library Python only; additional packages and network access are unavailable. Stop ends execution without blocking the lesson.

03. The idea

Try sketching your own approach first. What would you track, and why would it work?

Reveal the idea
  1. Read each weight once and calculate capacity - weight.
  2. Look for an earlier matching weight in the map.
  3. Store only the earliest position for a weight; stop at the first completed pair.

04. Watch it happen

Use Step to move slowly, or Play to follow the whole trace. Change a preset or load your own input.

Input & settings

Up to 30 positive integer weights and a positive capacity, each at most 1,000,000.

Red = current item / blue = active window or cells / green = completed work. A value docks at its actual destination; the original input stays unchanged. Positions start at 0.

Working state / what changes

Ready to begin

Enable JavaScript to step through the animation. The worked example and solution below remain readable without it.

Python / why it changes

The line behind the step

from typing import List

def pack_parcel(weights: List[int], capacity: int) -> List[int]:
    seen = {}
    for index, weight in enumerate(weights):
        partner = capacity - weight
        if partner in seen:
            return [seen[partner], index]
        seen.setdefault(weight, index)
    return []
How to use this walkthrough

Hover or focus a control for its label. Play forms the input and follows values into the working state. Step shows one change; Back and the slider restore exact snapshots. Reduced motion uses instant updates. Saved snapshots and sorted heap displays are teaching overhead, not algorithm complexity.

05. Python solution

from typing import List

def pack_parcel(weights: List[int], capacity: int) -> List[int]:
    seen = {}
    for index, weight in enumerate(weights):
        partner = capacity - weight
        if partner in seen:
            return [seen[partner], index]
        seen.setdefault(weight, index)
    return []

06. Cost & edge cases

Time
O(n) expected
Space
O(n)
  • Two separate 6-unit items fit capacity 12.
  • With [5, 5, 7], capacity 12 returns [0, 2], not [1, 2].
  • An empty belt or no fitting pair returns []. Weights and capacity must be positive integers.

Keep going