Decreasing Max Partitioning
Practice
3.5 (2 votes)
Medium
Dynamic programming
Problem
89% Success 1530 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You have been given an array A of N integers \(A_1, A_2 ... A_N\).
The Partition P of the array is defined by two integers \(startP\) and \(endP\) and it contains all the elements lying at positions \([startP, startP+1 ... endP]\). That is, partition P refers to subarray \([A_{startP}, A_{startP+1} ...A_{endP}]\) such that \(startP ≤ endP\).
The value of partition is defined as maximum element present in the partition i.e. \(value(P) = maximum(A_{startP}, A_{startP+1} ...A_{endP})\).

You have to find out total number of ways to divide array A into sequence of partitions such that each element of A belongs to exactly one partition and value of each partition is not less than the value of next partition.

Formally, Suppose you divided array A into k partitions \(P_1, P_2 ... P_k\) for some \(k ≥ 1\), then following conditions should be satisfied:
1. \(startP_1 = 1\) and \(endP_k = N\).
2. \(startP_i ≤ endP_i\) for all \(i ≤ k\).
3. \(startP_{i+1} = endP_i + 1\) for all \(i < k\).
4. \(value(P_i) ≥ value(P_{i+1})\) for all \(i < k\).

You have to find out total number of ways to divide array A into such sequence of partitions.

INPUT:
First line of input consists of integer T denoting total number of testcases.
First line of each test case consists of an integer N. Next line consists of N integers \(A_1, A_2 ... A_N\).

Output:
Output total number of ways to divide the given array into partitions. Since output can be large, print the total number of ways modulus \(10^9 + 7\).

Constraints:
\(1 ≤ T ≤ 20\)
\(1 ≤ N ≤ 10^5\)
\(1 ≤ A_i ≤ 10^5\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
15 votes
Tags:
Applications of Dynamic ProgrammingAlgorithmsDynamic ProgrammingDynamic programming
Points:30
Tags:
Medium
Points:30
2 votes
Tags:
Medium