Problem · Array

Find Number of Ways to Split Array

MediumGoogleNEW GRADOA
See Google hiring insights

You are given an array A of N integers. You can split the array into two non-empty parts, left and right, sort the elements in each part independently and join them back together.

Your task is to find the number of ways of splitting the array into two parts such that, after sorting the two parts and rejoining them into a single array, the resulting array will be sorted in non-decreasing order. For the array shown above, the answer is 2: the first and third splits result in a sorted array.

Examples
01 · Example 1
A = [1, 3, 2, 4]
return = 2

There are two ways to split the array such that the resulting array is sorted:

  • Splitting into left = [1] and right = [3, 2, 4], resulting in [1, 2, 3, 4].
  • Splitting into left = [1, 3, 2] and right = [4], resulting in [1, 2, 3, 4].

Therefore, the number of ways to split the array is 2.

Constraints
🍅🍅
More Google problems
drafts saved locally
public int findNumberOfWaysToSplitArray(int[] A) {
  // write your code here
}
A[1, 3, 2, 4]
expected2
sign in to submit