Count Beautiful Splits in an Array - Problem

You are given an integer array nums. Your task is to find all possible ways to split this array into three non-empty subarrays such that the split is considered "beautiful".

A split is beautiful if:

  1. The array nums is divided into exactly three consecutive subarrays: nums1, nums2, and nums3
  2. At least one of the following conditions is met:
    • nums1 is a prefix of nums2, OR
    • nums2 is a prefix of nums3

Note: Array A is a prefix of array B if A can be obtained by taking the first few elements of B. For example, [1, 2] is a prefix of [1, 2, 3, 4].

Goal: Return the total number of beautiful splits possible.

Input & Output

example_1.py โ€” Basic Case
$ Input: [1,1,2,1]
โ€บ Output: 2
๐Ÿ’ก Note: Two beautiful splits exist: (1) Split at positions 1,2 giving [1], [1], [2,1] where nums1 is prefix of nums2. (2) Split at positions 1,3 giving [1], [1,2], [1] where nums1 is prefix of nums2.
example_2.py โ€” No Beautiful Splits
$ Input: [1,2,3,4]
โ€บ Output: 0
๐Ÿ’ก Note: No matter how we split this array into three parts, neither prefix condition can be satisfied since all elements are distinct and increasing.
example_3.py โ€” Multiple Patterns
$ Input: [1,2,1,2,1,2]
โ€บ Output: 8
๐Ÿ’ก Note: Multiple beautiful splits are possible due to the repeating pattern [1,2]. Many combinations satisfy either nums1 being a prefix of nums2 or nums2 being a prefix of nums3.

Constraints

  • 3 โ‰ค nums.length โ‰ค 5000
  • 1 โ‰ค nums[i] โ‰ค 109
  • Each split must create exactly three non-empty subarrays

Visualization

Tap to expand
Beautiful Splits: Finding Prefix PatternsArray: [1, 2, 1, 2, 3, 1, 2]Step 1: Z-AlgorithmCompute prefix lengthsZ = [0,0,2,0,0,3,0]Step 2: Try All SplitsFor i=1 to n-2For j=i+1 to n-1Step 3: Check ConditionsUse Z[i] for prefix checkO(1) validation per splitExample Split Analysis: i=2, j=5nums1[1, 2]nums2[1, 2, 3]nums3[1, 2]Check: Is [1,2] prefix of [1,2,3]? โœ“Check: Is [1,2,3] prefix of [1,2]? โœ—Beautiful Split Found! (First condition satisfied)Time Complexity: O(n) preprocessing + O(nยฒ) checking = O(nยฒ)Space Complexity: O(n) for Z-array storage
Understanding the Visualization
1
Identify the Problem
We need to split array into 3 parts where either first is prefix of second OR second is prefix of third
2
Apply Z-Algorithm
Precompute all prefix relationships to avoid redundant comparisons
3
Check All Splits
For each valid split point combination, use precomputed values to check conditions
4
Count Valid Splits
Sum all combinations that satisfy at least one prefix condition
Key Takeaway
๐ŸŽฏ Key Insight: By preprocessing with Z-Algorithm, we transform expensive O(n) prefix checks into O(1) lookups, making the overall solution efficient enough for the given constraints.
Asked in
Google 15 Amazon 8 Meta 12 Microsoft 6
28.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen