Gamified challenge · Two pointers
weights lists the crates in the yard in non-decreasing order, and the truck carries exactly two crates whose weights must add up to exactly capacity.
Write pairSum(weights, capacity) (pair_sum in Python) that returns the 0-based indices [i, j] of two different crates with i < j and weights[i] + weights[j] == capacity. If several pairs work, any one is accepted. If none does, return [-1, -1].
For weights = [2, 7, 11, 15] and capacity = 9 the answer is [0, 1], because 2 + 7 = 9.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google