Skip to main content

Sum Of Special Evenly-Spaced Elements In Array - Solution & Explanation

HardPremiumFree on FleetCodeArrayDynamic Programming10 min readAsked at: MakeMyTrip
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: You receive an integer array and multiple queries [x, y]. Each query asks for the sum of elements starting at index x and jumping by step y: nums[x] + nums[x+y] + nums[x+2y] ... until the index exceeds the array length. The challenge is answering many such queries efficiently without recomputing the sequence every time.

Approach 1: Direct Simulation (Brute Force) (Time: O(q * n / y), Space: O(1))

The simplest method processes each query independently. Start at index x, repeatedly add nums[i], and increment the index by y until you leave the array. This works because the query directly describes the traversal pattern. However, if y is small, the loop may visit many elements, making the worst-case complexity close to O(q * n). This approach is useful for understanding the query structure but becomes too slow when both n and the number of queries are large.

Approach 2: Block Decomposition / Sqrt Optimization with DP (Time: O(n * sqrt(n) + q * sqrt(n)), Space: O(n * sqrt(n)))

The key observation: queries with small step sizes revisit many indices, while large step sizes naturally skip most of the array. Choose a threshold B = sqrt(n). For every step size y ≤ B, precompute answers using dynamic programming. Define dp[y][i] as the sum starting at index i with step y. Compute it backwards: dp[y][i] = nums[i] + (i + y < n ? dp[y][i + y] : 0). This preprocessing takes O(n * B) time.

During queries, if y ≤ B, return dp[y][x] instantly. If y > B, simulate the traversal because the number of visited elements is at most n / y, which is ≤ sqrt(n). This hybrid strategy keeps every query bounded by roughly O(sqrt(n)). The technique is a classic application of sqrt decomposition combined with dynamic programming over an array.

Recommended for interviews: The brute force method demonstrates understanding of the query pattern but does not scale. Interviewers expect the sqrt decomposition optimization. Recognizing that small step sizes repeat across queries and precomputing them with DP shows strong algorithmic intuition and familiarity with query optimization techniques.

Solution

This problem is a typical block decomposition problem. For queries with a large step size, we can directly brute force the solution; for queries with a small step size, we can preprocess the suffix sum of each position and then directly query.

In this problem, we limit the step size of the large step size query to \sqrt{n}, which can ensure that the time complexity of each query is O(\sqrt{n}).

We define a two-dimensional array suf, where suf[i][j] represents the suffix sum starting from position j with a step size of i. Then for each query [x, y], we can divide it into two cases:

  • If y \le \sqrt{n}, then we can directly query suf[y][x];
  • If y > \sqrt{n}, then we can directly brute force the solution.

The time complexity is O((n + m) times \sqrt{n}), and the space complexity is O(n times \sqrt{n}). Here, n is the length of the array, and m is the number of queries.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct Simulation (Brute Force)O(q * n / y) worst-case O(qn)O(1)Small inputs or very few queries where preprocessing is unnecessary
Block Decomposition + DPO(n√n + q√n)O(n√n)Large arrays and many queries; optimal approach used in competitive programming and interviews

Video Solution

Leetcode 1714. Sum Of Special Evenly-Spaced Elements In ArrayFearless Learner132 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Sum Of Special Evenly-Spaced Elements In Array easy or hard?
The problem is rated Hard because it requires recognizing a sqrt decomposition optimization. A naive solution is straightforward, but designing the preprocessing strategy and balancing time vs space for multiple queries requires deeper algorithmic insight.
Sum Of Special Evenly-Spaced Elements In Array Python/Java solution
Implement a DP table for step sizes up to √n and compute values backward: dp[y][i] = nums[i] + dp[y][i+y]. During queries, return dp[y][x] if the step size is small; otherwise iterate through the array. This approach works efficiently in Python, Java, C++, Go, and TypeScript.
How to solve Sum Of Special Evenly-Spaced Elements In Array in O(n√n)?
Use sqrt decomposition. Let B = √n and precompute dp[y][i] = nums[i] + dp[y][i+y] for every step size y ≤ B. This preprocessing takes O(n√n). When processing queries, return dp[y][x] if y ≤ B; otherwise iterate through indices x, x+y, x+2y until the array ends.
What is the best approach for Sum Of Special Evenly-Spaced Elements In Array?
The most efficient method uses block (sqrt) decomposition with dynamic programming. Precompute results for small step sizes y ≤ √n using a DP table where dp[y][i] stores the sum starting from index i with step y. For larger step sizes, simulate the traversal since at most √n elements are visited. This keeps total complexity around O(n√n + q√n).
Is Sum Of Special Evenly-Spaced Elements In Array asked at Google/Amazon/Meta?
Problems involving query optimization, sqrt decomposition, and dynamic programming over arrays commonly appear in interviews at companies like Google, Amazon, and Meta. This specific problem reflects the type of reasoning used in system queries and competitive programming style interview questions.
What data structure is used in Sum Of Special Evenly-Spaced Elements In Array?
The solution primarily uses arrays and a 2D dynamic programming table for precomputed step sizes. The technique combines array traversal with sqrt decomposition to split queries into precomputed and simulated cases.
What is the time complexity of Sum Of Special Evenly-Spaced Elements In Array?
The optimized solution runs in O(n√n + q√n) time with O(n√n) preprocessing space. Precomputation builds DP values for all step sizes up to √n, and each query either returns a precomputed value in O(1) or iterates at most √n elements when the step size is large.

Ready to solve this problem?

Practice Sum Of Special Evenly-Spaced Elements In Array with our built-in code editor and test cases.

Practice on FleetCode