algoblazerEarly Access
LearnProblemsStudy GroupsMock InterviewLeaderboard
Log inSign up

All problems

‹ Back to map
1 shown · 0/309 solved

Total of Smaller Predecessors

SilverCommunity Beta
Asked atEmerson
Solve problem →
2000ms256MBAdded Oct 6, 2026

You are given an integer array nums.

For each index i, consider every earlier index j with j < i and nums[j] < nums[i], and add nums[j] to a running total.

Return the total modulo 10 ** 9 + 7.

Examples

Example 1

Input: nums = [1,5,3,6,4]
Output: 15
Explanation: Index 1 (value 5) adds nums[0] = 1. Index 2 (value 3) adds nums[0] = 1. Index 3 (value 6) adds nums[0] + nums[1] + nums[2] = 1 + 5 + 3 = 9. Index 4 (value 4) adds nums[0] + nums[2] = 1 + 3 = 4. The total is 1 + 1 + 9 + 4 = 15.

Example 2

Input: nums = [5,4,3,2,1]
Output: 0
Explanation: Every later value is smaller than every earlier value, so no index ever finds an earlier value that is strictly smaller than itself.

Example 3

Input: nums = [2,2,3]
Output: 4
Explanation: Index 1 (value 2) finds no earlier value strictly smaller than 2. Index 2 (value 3) adds both earlier 2s: 2 + 2 = 4.

Constraints

  • 1≤1 \leq1≤ nums.length ≤105\leq 10^5≤105
  • 1≤1 \leq1≤ nums[i] ≤109\leq 10^9≤109

Details

Solved by2 people
Time limit2000ms
Memory256MB
AddedOct 6, 20263 hours ago