FastPrepSum Symmetric Pairwise Remainders

Sum Symmetric Pairwise Remainders

Confluent logoConfluent● MediumFULLTIMEOA
Learn

Problem statement

You are given a positive integer n. For positive integers i and j, define C(i, j) = (i % j) + (j % i), where % is the remainder after integer division.

Return the sum of C(i, j) over every ordered pair satisfying 1 <= i <= n and 1 <= j <= n, modulo 10^9 + 7.

Both (i, j) and (j, i) occur when i != j. Pairs with i = j are included and contribute 0.

Function

sumPairwiseRemainders(n: int) → int

Examples

Example 1

n = 2return = 2

C(1, 1) = 0, C(1, 2) = 1, C(2, 1) = 1, and C(2, 2) = 0. Their sum is 2.

Example 2

n = 1return = 0

The only pair is (1, 1), and both remainders are zero.

Example 3

n = 4return = 24

For divisors 1, 2, 3, and 4, the sums of i % j over i = 1..4 are 0, 2, 4, and 6. Their sum is 12. Swapping the two indices gives the same aggregate for j % i, so the required total is 24.

Constraints

  • 1 <= n <= 10^6.
  • Return an integer in [0, 10^9 + 6].

More Confluent problems

See Confluent hiring insights
public int sumPairwiseRemainders(int n) {
    // Write your code here.
}
n2
expected2
Checking account…