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 modulo10^9 + 7.
The floor() function returns the integer part of the division.
Input: nums =[2,5,9]Output: 10Explanation:
floor(2/5)= floor(2/9)= floor(5/9)=0floor(2/2)= floor(5/5)= floor(9/9)=1floor(5/2)=2floor(9/2)=4floor(9/5)=1We calculate the floor of the division for every pair of indices in the array then sum them up.
The key idea is to count, for each possible divisor, how many numbers in the array are divisible by it, and use prefix sums to efficiently compute the sum of floored pairs. Instead of brute-forcing all pairs, we use counting and prefix sums to reduce the time complexity.
For each possible divisor d from 1 to max_num, for each multiple of d, count how many numbers fall into that range and add count * freq[d] * k to the answer, where k is the floored value.
classSolution {
public:int sumOfFlooredPairs(vector<int>& nums) {
int mod =1e9+7;
int max_num =*max_element(nums.begin(), nums.end());
vector<int> freq(max_num +2, 0);
for (int n : nums) freq[n]++;
vector<int> prefix(max_num +2, 0);
for (int i =1; i <= max_num +1; ++i) prefix[i] = prefix[i-1] + freq[i];
int ans =0;
for (int d =1; d <= max_num; ++d) {
if (freq[d] ==0) continue;
for (int k =1; d * k <= max_num; ++k) {
int l = d * k, r = min(d * (k +1) -1, max_num);
int cnt = prefix[r] - prefix[l -1];
ans = (ans +1LL* freq[d] * cnt * k) % mod;
}
}
return ans;
}
};
classSolution {
publicintsumOfFlooredPairs(int[] nums) {
int mod = 1_000_000_007;
int maxNum = 0;
for (int n : nums) maxNum = Math.max(maxNum, n);
int[] freq =newint[maxNum + 2];
for (int n : nums) freq[n]++;
int[] prefix =newint[maxNum + 2];
for (int i = 1; i <= maxNum + 1; i++) prefix[i]= prefix[i-1]+ freq[i];
long ans = 0;
for (int d = 1; d <= maxNum; d++) {
if (freq[d]== 0) continue;
for (int k = 1; d * k <= maxNum; k++) {
int l = d * k, r = Math.min(d * (k + 1) - 1, maxNum);
int cnt = prefix[r]- prefix[l - 1];
ans = (ans + 1L * freq[d]* cnt * k) % mod;
}
}
return (int) ans;
}
}
classSolution {
funsumOfFlooredPairs(nums: IntArray): Int {
val mod = 1_000_000_007
val maxNum = nums.maxOrNull() ?:0val freq = IntArray(maxNum + 2)
for (n in nums) freq[n]++val prefix = IntArray(maxNum + 2)
for (i in1..maxNum+1) prefix[i] = prefix[i-1] + freq[i]
var ans = 0Lfor (d in1..maxNum) {
if (freq[d] ==0) continuevar k = 1while (d * k <= maxNum) {
val l = d * k
val r = minOf(d * (k + 1) - 1, maxNum)
val cnt = prefix[r] - prefix[l - 1]
ans = (ans + freq[d].toLong() * cnt * k) % mod
k++ }
}
return ans.toInt()
}
}
classSolution:
defsumOfFlooredPairs(self, nums: list[int]) -> int:
mod =10**9+7 max_num = max(nums)
freq = [0] * (max_num +2)
for n in nums:
freq[n] +=1 prefix = [0] * (max_num +2)
for i in range(1, max_num +2):
prefix[i] = prefix[i-1] + freq[i]
ans =0for d in range(1, max_num +1):
if freq[d] ==0:
continue k =1while d * k <= max_num:
l = d * k
r = min(d * (k +1) -1, max_num)
cnt = prefix[r] - prefix[l -1]
ans = (ans + freq[d] * cnt * k) % mod
k +=1return ans
impl Solution {
pubfnsum_of_floored_pairs(nums: Vec<i32>) -> i32 {
let m =*nums.iter().max().unwrap() asusize;
letmut freq =vec![0; m +2];
for&n in&nums {
freq[n asusize] +=1;
}
letmut prefix =vec![0; m +2];
for i in1..=m+1 {
prefix[i] = prefix[i-1] + freq[i];
}
letmut ans =0i64;
let modulo =1_000_000_007i64;
for d in1..=m {
if freq[d] ==0 { continue; }
letmut k =1;
while d * k <= m {
let l = d * k;
let r = (d * (k +1) -1).min(m);
let cnt = prefix[r] - prefix[l -1];
ans = (ans + freq[d] asi64* cnt asi64* k asi64) % modulo;
k +=1;
}
}
ans asi32 }
}
⏰ Time complexity: O(n + m log m) where n is the length of nums and m is the maximum value in nums. We count frequencies and build prefix sums in O(m), and for each divisor, the inner loop runs about O(log m) times, so total is O(m log m).
🧺 Space complexity: O(m) for the frequency and prefix sum arrays, where m is the maximum value in nums.