Bank app~10 min2s · 256 MBtarget O(n) time, O(n) spacetraversal
A bank app lets a person link accounts. A nightly job receives two long lists of balances: the first balance of each person and the second balance of the same person. The job must report the combined balance of every person, in the same order.
Balances are whole amounts in units, from 0 up to 1,000,000,000,000.
Write sums_of_pairs(first, second).
first[i] and second[i] are the two balances of person i. The lists have the same length.i is first[i] + second[i].Input: first = [12, 905, 40], second = [30, 95, 0]
Output: [42, 1000, 40]Read the pairs from the top. Each total comes from one row only.
| Person | first |
second |
Total |
|---|---|---|---|
| 0 | 12 | 30 | 42 |
| 1 | 905 | 95 | 1000 |
| 2 | 40 | 0 | 40 |
Input: first = [1000000000000], second = [1000000000000]
Output: [2000000000000]A single pair. The total, 2,000,000,000,000, is larger than the biggest 32-bit integer, 2,147,483,647.
Input: first = [0, 7], second = [0, 0]
Output: [0, 7]Two people with no money. The totals are 0 and 7.
1 ≤ length of first = length of second ≤ 2 x 10^50 ≤ first[i], second[i] ≤ 10^12A balance can have 13 digits, which is more than a 32-bit integer holds (about 2.1 x 10^9). The total of two balances can reach 2 x 10^12. Use a 64-bit number for the total.
The lists can hold 200,000 pairs. One addition for each pair is about 2 x 10^5 steps, so the job is fast as long as nothing in the loop costs more than a step.