<p>Given an integer array <code>nums</code>, return the sum of <code>floor(nums[i] / nums[j])</code> for all pairs of indices <code>0 &lt;= i, j &lt; nums.length</code> in the array. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>The <code>floor()</code> function returns the integer part of the division.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,5,9]
<strong>Output:</strong> 10
<strong>Explanation:</strong>
floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0
floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1
floor(5 / 2) = 2
floor(9 / 2) = 4
floor(9 / 5) = 1
We calculate the floor of the division for every pair of indices in the array then sum them up.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [7,7,7,7,7,7,7]
<strong>Output:</strong> 49
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

#
# @lc app=leetcode id=1862 lang=python3
#
# [1862] Sum of Floored Pairs
#
# https://leetcode.com/problems/sum-of-floored-pairs/description/
#
# algorithms
# Hard (28.87%)
# Likes:    438
# Dislikes: 37
# Total Accepted:    9.3K
# Total Submissions: 32.4K
# Testcase Example:  '[2,5,9]'
#
# 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.
#
#
# Example 1:
#
#
# Input: nums = [2,5,9]
# Output: 10
# Explanation:
# floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0
# floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1
# floor(5 / 2) = 2
# floor(9 / 2) = 4
# floor(9 / 5) = 1
# We calculate the floor of the division for every pair of indices in the array
# then sum them up.
#
#
# Example 2:
#
#
# Input: nums = [7,7,7,7,7,7,7]
# Output: 49
#
#
#
# Constraints:
#
#
# 1 <= nums.length <= 10^5
# 1 <= nums[i] <= 10^5
#
#
#

MOD = 10**9 + 7

class Solution:
    def sumOfFlooredPairs(self, nums: List[int]) -> int:
        num_nums = max(nums)
        prefix_freq = collections.Counter(nums)
        for idx in range(1, num_nums + 1):
            prefix_freq[idx] += prefix_freq[idx - 1]

        res = 0
        for idx in range(1, num_nums + 1):
            if prefix_freq[idx] - prefix_freq[idx - 1] > 0:
                sum_val = 0
                for j in range(1, (num_nums // idx) + 1):
                    low = idx * j
                    high = min(idx * (j + 1) - 1, num_nums)
                    count = prefix_freq[high] - prefix_freq[low - 1]
                    sum_val = (sum_val + (count * j) % MOD) % MOD
                curr = prefix_freq[idx] - prefix_freq[idx - 1]
                res = (res + (curr * sum_val) % MOD) % MOD
        return res


37300