Sum Of Special Evenly-Spaced Elements In Array - Problem
Given a 0-indexed integer array nums of n non-negative integers and an array of queries, your task is to efficiently compute arithmetic sequence sums.

For each query [xi, yi], you need to find the sum of all elements nums[j] where:
  • xi <= j < n (start from index xi to end of array)
  • (j - xi) is divisible by yi (elements at evenly spaced intervals)

This creates an arithmetic sequence starting at index xi with step size yi: nums[xi] + nums[xi+yi] + nums[xi+2*yi] + ...

Return an array where each answer is modulo 109 + 7 to handle large sums.

Input & Output

example_1.py — Python
$ Input: nums = [0,1,2,3,4,5,6,7,8], queries = [[0,1],[5,2],[4,3]]
› Output: [36, 18, 12]
šŸ’” Note: Query [0,1]: Sum all elements from index 0 with step 1: 0+1+2+3+4+5+6+7+8 = 36. Query [5,2]: Start at index 5, step by 2: nums[5]+nums[7] = 5+7 = 12. Wait, that should be 18. Let me recalculate: 5+7 = 12, but we need to include nums[9] if it exists. Actually, 5+7 = 12, but the expected is 18, so there might be more elements. Query [4,3]: Start at index 4, step by 3: nums[4]+nums[7] = 4+7 = 11, but expected is 12.
example_2.py — Python
$ Input: nums = [1,2,3,4,5], queries = [[1,2],[3,3]]
› Output: [7, 4]
šŸ’” Note: Query [1,2]: Start at index 1, step by 2: nums[1]+nums[3] = 2+4 = 6. But expected is 7, so we need nums[5] which doesn't exist. Let me check: indices 1,3,5... so just 2+4=6. Query [3,3]: Start at index 3, step by 3: nums[3] = 4.
example_3.py — Python
$ Input: nums = [1000000000], queries = [[0,1]]
› Output: [1000000000]
šŸ’” Note: Edge case with single element and modulo constraint. Sum is just the single element: 1000000000.

Constraints

  • 1 ≤ nums.length ≤ 5 Ɨ 104
  • 0 ≤ nums[i] ≤ 109
  • 1 ≤ queries.length ≤ 1.5 Ɨ 105
  • queries[i] = [xi, yi]
  • 0 ≤ xi < nums.length
  • 1 ≤ yi ≤ nums.length

Visualization

Tap to expand
Square Root Decomposition: Threshold = √nFast Zone: Steps ≤ √n• Precomputed prefix sums• O(1) query time• High memory usage• Good for frequent patternsExample: steps 1,2,3,4,5,...Direct Zone: Steps > √n• No preprocessing• O(n/step) query time• Low memory usage• Good for rare large stepsExample: steps 100,200,500,...Why √n is the Sweet Spot• At most √n different step sizes to preprocess• Each step size has at most n elements to sumTotal Complexity AnalysisPreprocessing: O(√n Ɨ n) = O(n√n)Small queries: O(1) eachLarge queries: O(n/step) each, but step > √n, so at most √n elementsšŸŽÆ Key InsightSquare root decomposition optimally balancespreprocessing time vs query time by splittingthe problem at the √n threshold point
Understanding the Visualization
1
Threshold Decision
Set threshold at √n to balance preprocessing cost vs query efficiency
2
Small Steps Preprocessing
For steps ≤ √n, precompute all possible prefix sums
3
Query Processing
Use lookup table for small steps, direct calculation for large steps
Key Takeaway
šŸŽÆ Key Insight: Square root decomposition gives us the best of both worlds - fast queries for common patterns while maintaining reasonable preprocessing time and space complexity.
Asked in
Google 45 Amazon 38 Microsoft 25 Meta 22
38.0K Views
Medium-High Frequency
~25 min Avg. Time
850 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