Sum of Floored Pairs - Problem

Given an integer array nums, return the sum of floor(nums[i] / nums[j]) for all pairs of indices 0 <= i, j < nums.length in the array.

Since the answer may be too large, return it modulo 10^9 + 7.

The floor() function returns the integer part of the division.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,5,1]
Output: 12
💡 Note: All pairs: floor(2/2)=1, floor(2/5)=0, floor(2/1)=2, floor(5/2)=2, floor(5/5)=1, floor(5/1)=5, floor(1/2)=0, floor(1/5)=0, floor(1/1)=1. Sum = 1+0+2+2+1+5+0+0+1 = 12
Example 2 — Small Array
$ Input: nums = [7,7,7,7,7,7,7]
Output: 49
💡 Note: All elements are 7, so floor(7/7)=1 for all 49 pairs (7×7), giving sum = 49
Example 3 — Edge Case
$ Input: nums = [1,1]
Output: 2
💡 Note: Two pairs: floor(1/1)=1, floor(1/1)=1. Sum = 1+1 = 2

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 105

Visualization

Tap to expand
Sum of Floored Pairs: Calculate floor(a/b) for all pairs251i=0i=1i=2Calculate all pairs:floor(2/2)=1, floor(2/5)=0, floor(2/1)=2floor(5/2)=2, floor(5/5)=1, floor(5/1)=5floor(1/2)=0, floor(1/5)=0, floor(1/1)=1Sum = 1+0+2+2+1+5+0+0+1 = 12Result: 12
Understanding the Visualization
1
Input
Array of integers: [2,5,1]
2
Process
Calculate floor(nums[i]/nums[j]) for all pairs (i,j)
3
Output
Sum all floor division results
Key Takeaway
🎯 Key Insight: Instead of computing each pair individually, group by frequency or use binary search to handle ranges of similar quotients efficiently
Asked in
Google 15 Microsoft 12 Amazon 8
23.5K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen