You are given an integer n.
A sequence is formed as follows:
1st block contains 1.2nd block contains 2 * 3.ith block is the product of the next i consecutive integers.Let F(n) be the sum of the first n blocks.
Return an integer denoting F(n) modulo 109 + 7.
Example 1:
Input: n = 3
Output: 127
Explanation:
12 * 3 = 64 * 5 * 6 = 120F(3) = 1 + 6 + 120 = 127
Example 2:
Input: n = 7
Output: 6997165
Explanation:
12 * 3 = 64 * 5 * 6 = 1207 * 8 * 9 * 10 = 504011 * 12 * 13 * 14 * 15 = 36036016 * 17 * 18 * 19 * 20 * 21 = 3907008022 * 23 * 24 * 25 * 26 * 27 * 28 = 5967561600F(7) = 6006997207 % (109 + 7) = 6997165
Constraints:
1 <= n <= 1000Problem Overview: You process numbers in sequential blocks where the block size increases each step (1, 2, 3, ...). For every block, compute the product of its elements and add it to a running total. Continue until all elements are consumed.
Approach 1: Brute Force Block Product (O(n2) time, O(1) space)
The most direct method explicitly forms each block and recomputes the product from scratch. Start with block size k = 1, multiply the next k elements to get the block product, add it to the answer, then increment k. Because the product is recomputed element‑by‑element for every block and may overlap repeated scanning in naive implementations, the worst case grows to O(n2) time. Space usage stays O(1) since only counters and a running product are stored. This approach is useful for validating logic or small constraints but becomes inefficient for larger inputs.
Approach 2: Direct Simulation with Running Block Size (O(n) time, O(1) space)
A more practical strategy simulates the process in a single pass. Maintain a pointer to the current index and a variable blockSize that increases after every block. For each block, iterate exactly blockSize elements (or until the array ends), compute the product, add it to the total, and move the pointer forward. Each element participates in exactly one multiplication sequence, so the total work across all blocks is O(n). The algorithm only stores a few integers such as the pointer, product accumulator, and block counter, which keeps the space complexity at O(1). This approach fits naturally under simulation problems and relies on simple arithmetic reasoning from math concepts.
Recommended for interviews: Interviewers typically expect the linear simulation approach. Showing the brute force idea demonstrates you understand the block construction, but implementing the single‑pass simulation with increasing block sizes proves you can translate a mathematical pattern into efficient code. The optimal solution runs in O(n) time and O(1) space, which is ideal for constraints commonly seen in simulation and math style interview questions.
We can directly simulate the product of each block and accumulate it to the answer. Note that since the product can be very large, we need to take the modulo at each step of the calculation.
The time complexity is O(n^2), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Block Product | O(n²) | O(1) | Useful for understanding the block structure or when constraints are very small |
| Single Pass Simulation | O(n) | O(1) | Best general solution; processes each element once with increasing block sizes |
Practice Sum of Increasing Product Blocks with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor