The problem
Return how many pairs of positions i < j in the list a have a sum a[i] + a[j] that is a multiple of k.
pairs_to_multiple([1, 2, 3, 4, 5], 3) # 4
pairs_to_multiple([-1, 1, -5, 7], 6) # 3
pairs_to_multiple([5] * 10, 10) # 45
Limits
ahas up to 200,000 whole numbers, which may be negative and may be very large.kis from 1 to about a million.- Checking every pair is far too slow for 200,000 numbers (about 20 billion pairs). The big tests have 3 seconds each.
Builds on MATH-2 Modular arithmetic.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
12 tests, not run yet
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | The example | Not run | |
| 2 | No values | Not run | |
| 3 | k is 1 | Not run | |
| 4 | All remainder 0 | Not run | |
| 5 | Remainder k/2 | Not run | |
| 6 | Negative values | Not run | |
| 7 | Repeated value | Not run | |
| 8 | Big values | Not run | |
| 9 | 2,000 values | Not run | |
| 10 | 200,000 values | Not run | |
| 11 | k is 2 | Not run | |
| 12 | All multiples of 7 | Not run |