Talentd
FleetCode

1714. Sum Of Special Evenly-Spaced Elements In Array

Hard49.9% acceptance1 companyPremium elsewhere, free here

You are given a 0-indexed integer array nums consisting of n non-negative integers.

You are also given an array queries, where queries[i] = [xi, yi]. The answer to the ith query is the sum of all nums[j] where xi <= j < n and (j - xi) is divisible by yi.

Return an array answer where answer.length == queries.length and answer[i] is the answer to the ith query modulo 109 + 7.

Example 1:

Input: nums = [0,1,2,3,4,5,6,7], queries = [[0,3],[5,1],[4,2]]
Output: [9,18,10]
Explanation: The answers of the queries are as follows:
1) The j indices that satisfy this query are 0, 3, and 6. nums[0] + nums[3] + nums[6] = 9
2) The j indices that satisfy this query are 5, 6, and 7. nums[5] + nums[6] + nums[7] = 18
3) The j indices that satisfy this query are 4 and 6. nums[4] + nums[6] = 10

Example 2:

Input: nums = [100,200,101,201,102,202,103,203], queries = [[0,7]]
Output: [303]

Constraints:

  • n == nums.length
  • 1 <= n <= 5 * 104
  • 0 <= nums[i] <= 109
  • 1 <= queries.length <= 1.5 * 105
  • 0 <= xi < n
  • 1 <= yi <= 5 * 104

Asked at 1 company

Hints open one at a time

Hint 1
Think if y cannot be small. You can solve a query in O(n/y), so if y is large enough, it won't be a problem.
Hint 2
If y is small, like less than B, you can preprocess the answers for all such ys in O(n * B), then answer each such query in O(1).
Hint 3
As you might have already guessed, the optimal value for B is ~sqrt(n).
24:47
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[0,1,2,3,4,5,6,7]
[[0,3],[5,1],[4,2]]
Input
[100,200,101,201,102,202,103,203]
[[0,7]]
Your code is saved on this device.Solution