Problem · Array
Count Sortable Splits
Learn this problemProblem statement
You are given an integer array A of length N.
Choose one split position that divides A into two non-empty contiguous parts, called left and right. Sort the elements in each part independently in non-decreasing order, then join the sorted left part followed by the sorted right part.
Return the number of split positions for which the joined array is sorted in non-decreasing order.
Function
solution(A: int[]) → intExamples
Example 1
A = [1, 3, 2, 4]return = 2There are three possible split positions:
left = [1]andright = [3, 2, 4]produce[1, 2, 3, 4], so this split works.left = [1, 3]andright = [2, 4]produce[1, 3, 2, 4], so this split does not work.left = [1, 3, 2]andright = [4]produce[1, 2, 3, 4], so this split works.
Therefore, the answer is 2.