Opening your workspace…
Workspace/Coding room
Opening your workspace…
The winter sampler comes in a box with room for two coffee bags. Its brass latch adds the numbers on their labels and opens only when the sum equals target. Given numbers in shelf order, return the zero-based shelf indices of the two bags that open the latch, smaller index first. Exactly one pair of distinct bags works. Pip has already tried showing it the same bag twice.
[3,8,4,6], 10Output[2,3]Bags 2 and 3 hold 4 and 6, which add up to 10.
[5,5], 10Output[0,1]Two bags can hold the same number, as long as they are different bags.
Keep a map of the numbers you have already passed. For each bag, check whether the number it still needs is in the map.
Practice scanning arrays while preserving their original order.
Try this problem →Class B · Strings / Sliding windowReuse a map to remember where a character last appeared.
Try this problem →Class C · Arrays / Hash mapsUse a map to count occurrences before choosing an index.
Try this problem →/** @param {number[]} numbers * @param {number} target * @returns {number[]} */ function solve(numbers, target) { // Find the two indices that add up to target. }
Tab inserts two spaces. Shift+Tab outdents. Use the arrow keys and Tab to choose a suggestion. Press Escape, then Tab, to leave the editor. Press Control or Command with apostrophe to run samples or with Enter to submit.
[3,8,4,6], 10[2,3]