Ctrl K

Even Out the Shelves

Easy
You and a friend are trading books. The array mine holds the page count of each of your books, and theirs holds the page counts of your friend's books. Right now your page totals differ. You want to trade exactly one book each so that afterwards you both own the same total number of pages. Return [a, b], where a is the page count of the book you give away and b is the page count of the book your friend gives you. If several trades work, pick the one whose book appears earliest in mine. At least one valid trade is guaranteed.
Example 1
mine=[5, 2], theirs=[3] → [5, 3]

You have 7 pages and your friend has 3. Give away the 5-page book and take the 3-page one: you both end up with 5.

Example 2
mine=[4, 6, 1], theirs=[2, 5] → [4, 2]

The totals start at 11 and 7. Trading 4 for 2 gives 9 and 9; no other trade works.

Example 3
mine=[3, 1, 4], theirs=[2, 2, 4, 6] → [3, 6]

The totals are 8 and 14, so the book you get must be 3 pages longer than the one you give. Both 3 ↔ 6 and 1 ↔ 4 work; the 3-page book comes first in your list, so the answer is [3, 6].

Constraints
  • 1 ≤ len(mine), len(theirs) ≤ 10⁴
  • 1 ≤ mine[i], theirs[j] ≤ 10⁵
  • Your page totals are different
  • A valid trade exists
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Lost Ticket EasyValues in Both Lists EasyShared Values With Repeats EasyTwice as Much EasyPairs With a Fixed Gap Medium
loading Python…
Input
mine=[5, 2], theirs=[3]
Expected
[5, 3]

Run checks the first 3 tests in your browser. Submit checks all 24 tests on the server, hidden ones included.