FastPrepFastPrep
Problem Brief

Checksum Aggregator

OA

In a distributed network system, data packets with unique identifiers and checksum values are sent between nodes to maintain communication and data integrity. Develop a function to aggregate the checksum values for all possible pairs of packets within the network.

For any two distinct packets with identifiers i and j, the checksum is calculated as C(i, j) = i mod j + j mod i. Calculate the sum of C(i, j) for all pairs (1 <= i <= n) and (1 <= j <= n), where n is the total number of packets in the network. Return the result modulo (10 ** 9 + 7)

Function Description

Complete the function computeChecksumAggregation in the editor.

computeChecksumAggregation has the following parameter:

  1. int n: the total number of packets in the network

Returns

int: the aggregated checksum

My sincere thanks to da best friend for so generouly sharing the problem source! 🌷

1Example 1

Input
n = 3
Output
10
Explanation

Given n = 3, calculate the checksum for all the pairs of packets (i, j) where (1 <= i <= n) and (1 <= j <= n)

  • C(1, 1) = 1 mod 1 + 1 mod 1 = 0 + 0 = 0
  • C(1, 2) = 1 mod 2 + 2 mod 1 = 1 + 0 = 1
  • C(1, 3) = 1 mod 3 + 3 mod 1 = 1 + 0 = 1
  • C(2, 1) = 2 mod 1 + 1 mod 2 = 0 + 1 = 1
  • C(2, 2) = 2 mod 2 + 2 mod 2 = 0 + 0 = 0
  • C(2, 3) = 2 mod 3 + 3 mod 2 = 2 + 1 = 3
  • C(3, 1) = 3 mod 1 + 1 mod 3 = 0 + 1 = 1
  • C(3, 2) = 3 mod 2 + 2 mod 3 = 1 + 2 = 3
  • C(3, 3) = 3 mod 3 + 3 mod 3 = 0 + 0 = 0

The aggregated checksum is 0 + 1 + 1 + 1 + 0 + 3 + 1 + 3 + 0 = 10 mod (10 ** 9 + 7) = 10

Constraints

Limits and guarantees your solution can rely on.

1 <= n <= 10 ** 6
public int computeChecksumAggregation(int n) {
  // write your code here
}
Input

n

3

Output

10

Sign in to submit your solution.