Minimum Index of a Valid Split - Problem

You're given an integer array where one element dominates more than half the positions. Your task is to find the minimum index where you can split this array into two parts, such that both parts have the same dominant element.

A dominant element is one that appears in more than half the positions of an array. For example, in [3, 3, 3, 2, 2], the number 3 is dominant because it appears 3 times out of 5 positions (more than 2.5).

Goal: Find the smallest index i where splitting at position i creates two subarrays that both have the same dominant element as the original array.

Input: An integer array nums with exactly one dominant element

Output: The minimum valid split index, or -1 if no valid split exists

Example: nums = [1,1,1,2,2,2,2] โ†’ The dominant element is 2. We need to find where to split so both parts are dominated by 2.

Input & Output

example_1.py โ€” Basic Split
$ Input: nums = [1,1,1,2,2,2,2]
โ€บ Output: 4
๐Ÿ’ก Note: The dominant element is 2 (appears 4/7 times). Split at index 4: Left=[1,1,1,2,2] has 2 appearing 2/5 times (dominant), Right=[2,2] has 2 appearing 2/2 times (dominant). Both sides have the same dominant element 2.
example_2.py โ€” Early Split
$ Input: nums = [2,1,3,1,1,1,7,1,2,1]
โ€บ Output: 4
๐Ÿ’ก Note: The dominant element is 1 (appears 6/10 times). Split at index 4: Left=[2,1,3,1,1] has 1 appearing 3/5 times (dominant), Right=[1,7,1,2,1] has 1 appearing 3/5 times (dominant). This is the minimum valid split.
example_3.py โ€” No Valid Split
$ Input: nums = [3,3,3,3,7,2,2]
โ€บ Output: -1
๐Ÿ’ก Note: The dominant element is 3 (appears 4/7 times). However, no split creates two subarrays where both have 3 as dominant. For example, at any split, either the left or right side won't have enough 3's to be dominant.

Constraints

  • 3 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • nums contains exactly one dominant element

Visualization

Tap to expand
๐Ÿ† Championship Tournament SplitFind the minimum split where both sections are dominated by the winning teamT1T2T2T2T1T2T2Team 2 (Green) is the dominant team with 5/7 fansSplit LineLeft SectionT1: 1 fan, T2: 2 fansT2 Dominates (2/3)Right SectionT1: 1 fan, T2: 3 fansT2 Dominates (3/4)โœ… Valid Split Found!Both sections are dominated by Team 2This is the minimum (leftmost) valid split point
Understanding the Visualization
1
Identify the Champions
Walk through the crowd and use Boyer-Moore voting to efficiently identify which team has the most fans without counting everyone individually
2
Find the Perfect Split
Move through the crowd from left to right, keeping track of how many champion fans are on each side of potential split points
3
Verify Dominance
For each potential split, check if the champion team has more than half the fans on both the left and right sides
4
Choose Minimum Split
Return the earliest (leftmost) position where both sections have the champion team as dominant
Key Takeaway
๐ŸŽฏ Key Insight: We only need to track the dominant element's count. If it has majority on both sides of a split, then no other element can dominate either side, making it a valid split. Use Boyer-Moore to find the dominant element efficiently, then find the minimum split in a single pass.
Asked in
Google 42 Microsoft 38 Amazon 31 Meta 28
42.4K Views
Medium Frequency
~22 min Avg. Time
1.8K 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