Sum of Pairwise Modulo Checksums (i mod j + j mod i) for All Pairs up to n
Company: IBM
Role: Business Intelligence Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
A distributed network system has `n` data packets whose identifiers are the integers `1` through `n`. For two identifiers `i` and `j`, the checksum of the pair is
```text
C(i, j) = (i mod j) + (j mod i)
```
where `a mod b` is the remainder when `a` is divided by `b` (Python's `a % b`). Compute the sum of `C(i, j)` over every pair with `1 <= i <= n` and `1 <= j <= n`, and return it modulo `10^9 + 7`.
### Function Signature
```python
def compute_checksum_aggregation(n: int) -> int:
```
### Rules
- Pairs are ordered: `(i, j)` and `(j, i)` are both counted when `i != j`, and the `n` pairs with `i == j` are included as well (each has checksum 0).
- Add up all `n * n` checksums exactly, then return the total modulo `1000000007`, an integer in `[0, 1000000006]`.
### Constraints
- `1 <= n <= 10^6`
- For the largest inputs the unreduced total is far beyond `2^31 - 1` and also beyond `2^53`; only its remainder modulo `10^9 + 7` is returned.
### Examples
**Example 1**
```text
Input: n = 2
Output: 2
```
`C(1, 1) = 0`, `C(1, 2) = 1 + 0 = 1`, `C(2, 1) = 0 + 1 = 1` and `C(2, 2) = 0`, for a total of 2.
**Example 2**
```text
Input: n = 3
Output: 10
```
| C(i, j) | j = 1 | j = 2 | j = 3 |
| --- | --- | --- | --- |
| i = 1 | 0 | 1 | 1 |
| i = 2 | 1 | 0 | 3 |
| i = 3 | 1 | 3 | 0 |
For example, `C(2, 3) = (2 mod 3) + (3 mod 2) = 2 + 1 = 3`. The nine values sum to 10.
**Example 3**
```text
Input: n = 4
Output: 24
```
Summing `C(i, j)` over `j = 1, 2, 3, 4` gives 3 for `i = 1`, 6 for `i = 2`, 8 for `i = 3` and 7 for `i = 4`, for a total of 24.
Overview: Compute the sum of (i mod j) + (j mod i) over every ordered pair of integers i and j from 1 to n, returned modulo 10^9 + 7, for n up to one million. It tests reasoning about remainders and designing an algorithm far faster than evaluating all n squared pairs.
Read the full IBM Business Intelligence Engineer interview experience this question came from