Problem Statement
Captain Ascending has discovered an ancient treasure map that reveals hidden fortunes along a mystical coastline.
The map is represented by an array A of length N, where each element A[i] indicates the treasure value (which may be negative or positive) at that location.
To unlock the ultimate riches, the captain believes his journey must be divided into segments in which the sum of treasures in each segment (called its "beauty") strictly increases as he moves from one segment to the next. The beauty of a range [L, R] in the array A is defined as the sum of the elements from A[L] to A[R].
The captain wishes to partition the array A into non-intersecting, contiguous subarrays such that, when each subarray is replaced by its beauty, the resulting sequence forms a strictly increasing progression.
Find the total number of ways to partition the array A into contiguous subarrays so that the sequence of beauties is strictly increasing. Since the answer can be very large, return it modulo 10^9 + 7.
Input Format
- The first line contains an integer,
N, denoting the number of elements inA. - Each of the
Nsubsequent lines (where 0 <= i < N) contains an integer describingA[i].
Constraints
1 <= N <= 1000-10^9 <= A[i] <= 10^9
Sample Test Cases
Case 1
- Input:
212
- Output:
2
- Explanation:
- For the array
[1][2], there are two valid partitions:- Single segment partition:
- Partition:
[1][2] - Beauty: 3
- With only one segment, the condition is automatically satisfied.
- Partition:
- Two segment partition:
- Partition:
[1]and[2] - Beauties: 1 and 2, which is strictly increasing (since 1 < 2).
- Partition:
- Single segment partition:
- Thus, the total number of valid partitions is 2.
- For the array








Dp state is : I am standing currently at index i, and I want subarray ending at me with sum = j. Then, this dp state will tell me, how many ways are there to solve the question for array a[0...i]. So, there are n^2 states possible.
You were iterating from left to right. You will put the subarray (I am considering subarrays with right index < i) sums possible till now in a pbds ordrered_set (or something else), and easily get the count of sums less than my desired sum, that is j. This count will go into dp[i][j].
Answer will be sum of dp[n — 1][j] for all possible j's at n — 1 index.
Tip: Use map for storing the second dimension in dp state.
If there is any mistake in my approach, please point out.