Lv.1arraymathprefix-sum
Sum of Pairwise Products
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
For every pair of distinct indices i < j of skill, the pair scores skill[i] × skill[j]. Return the sum of the scores of
all pairs modulo .
Examples
| skill | result |
|---|---|
[1, 2, 3] | 11 |
[4, 0, 5, 1] | 29 |
[1000000000, 1000000000] | 49 |
Example 1 — 1×2 + 1×3 + 2×3 = 11.
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(skill: int[]) → int
Constraints
-
skill.length -
skill[i] - Return the answer modulo .
Expected complexity
- Time
- O(N)
- Space
- O(1)
Examples
| skill | Expected |
|---|---|
[1,2,3] | 11 |
[4,0,5,1] | 29 |
[1000000000,1000000000] | 49 |
Hint
Hint
Fixing j, its pairs with earlier members sum to . Keep the
running sum (mod ) while sweeping left to right, and use 64-bit multiplication. Multiplying every pair is and
exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 10 | verdict only |
| performance | 60 | 3 | verdict only |