678. Sum Of Special Evenly-Spaced Elements In Array

Given a 0-indexed integer array nums consisting of n non-negative integers and another array queries, where each query is represented as queries[i] = [xi, yi]. For each query, compute the sum of all elements nums[j] where xi ≤ j < n and (j - xi) is divisible by yi.

Return an array answer where answer[i] contains the sum computed for the i-th query, taken modulo 109+ 7.

Example 1:

Input: nums = [2, 3, 5, 7, 11, 13, 17, 19], queries = [[1, 2], [4, 3], [2, 4]]  

Output: [42, 30, 22]

Explanation:

For queries[0] = [1, 2], the valid indices are {1, 3, 5, 7} → nums[1] + nums[3] + nums[5] + nums[7] = 3 + 7 + 13 + 19 = 42

For queries[1] = [4, 3], the valid indices are {4, 7} → nums[4] + nums[7] = 11 + 19 = 30

For queries[2] = [2, 4], the valid indices are {2, 6} → nums[2] + nums[6] = 5 + 17 = 22

Example 2:

Input: nums = [50, 60, 70, 80, 90, 100, 110, 120], queries = [[0, 4], [3, 2]]  

Output: [140, 300]

Explanation:

For queries[0] = [0, 4], valid indices are {0, 4} → nums[0] + nums[4] = 50 + 90 = 140

For queries[1] = [3, 2], valid indices are {3, 5, 7} → nums[3] + nums[5] + nums[7] = 80 + 100 + 120 = 300

Now Your Turn!

Pick the correct output for the given input

Input: nums = [10, 20, 30, 40, 50, 60, 70, 80], queries = [[2, 3]]  

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
vector<int> SumofSpecialElements(vector<int>& nums, vector<vector<int>>& queries) {
}
};
Test Case

Input:

Nums
Queries