Minimum Index of a Valid Split - Problem

An element x of an integer array arr of length m is dominant if more than half the elements of arr have a value of x.

You are given a 0-indexed integer array nums of length n with one dominant element.

You can split nums at an index i into two arrays nums[0, ..., i] and nums[i + 1, ..., n - 1], but the split is only valid if:

  • 0 <= i < n - 1
  • nums[0, ..., i], and nums[i + 1, ..., n - 1] have the same dominant element.

Return the minimum index of a valid split. If no valid split exists, return -1.

Input & Output

Example 1 — Basic Valid Split
$ Input: nums = [10,5,2,10,10]
Output: 2
💡 Note: Split at index 2: left=[10,5,2] has dominant 10 (count=1 > 3/2), right=[10,10] has dominant 10 (count=2 > 2/2). Both parts have the same dominant element 10.
Example 2 — No Valid Split
$ Input: nums = [2,1,3,1,1,1,7,1,2,1]
Output: -1
💡 Note: The dominant element is 1 (appears 6 times > 10/2). However, no split position creates two parts where 1 is dominant in both parts simultaneously.
Example 3 — Early Valid Split
$ Input: nums = [3,3,3,3,7,2,2]
Output: 0
💡 Note: Split at index 0: left=[3] has dominant 3, right=[3,3,3,7,2,2] has dominant 3 (count=3 > 6/2). Valid split found at the earliest possible position.

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109
  • nums has exactly one dominant element

Visualization

Tap to expand
Minimum Index of Valid Split: Find Where Both Parts Have Same Dominant1052101001234Dominant element: 10 (appears 3 times)Valid split at index 2Left: [10,5,2]Size: 310 appears 1 time1 > 3/2 ✓ DominantRight: [10,10]Size: 210 appears 2 times2 > 2/2 ✓ DominantOutput: 2 (minimum valid split index)
Understanding the Visualization
1
Input Array
[10,5,2,10,10] with dominant element 10 (appears 3 > 5/2 times)
2
Find Valid Split
Try split positions until both parts have same dominant element
3
Return Index
Minimum valid split index is 2
Key Takeaway
🎯 Key Insight: The array's dominant element must be the dominant element in both split parts
Asked in
Google 15 Meta 12 Amazon 8
8.5K Views
Medium Frequency
~15 min Avg. Time
234 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