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 - 1nums[0, ..., i], andnums[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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code