Consider there is an array a - we need to find the largest prefix of a such that it is good.
The array b of length m is called good, if you can obtain a non-decreasing array c (c1 ≤ c2 ≤ ⋯ ≤ cm) from it, repeating the following operation m times (initially, select either the first or the last element of b, remove it from b, and append it to the end of the array c).
I could only brute force the solution but is there any effective algorithm?