Find the smallest split point where the same dominant value remains dominant in both parts of the array.
You are given an integer array nums. A split index i divides the array into two non-empty parts: nums[0..i] and nums[i+1..n-1].
A value is called the dominant value of a segment if it appears in more than half of the segment's elements.
Return the minimum split index i such that:
- the left part has a dominant value, and
- the right part has a dominant value, and
- both dominant values are the same number.
If no such split exists, return -1.
The array may contain repeated values, and the dominant value is not necessarily unique in the original array description, but if a valid split exists, the same value must dominate both sides.
Input Format
Input
- An integer array
numsof lengthn.
Interpretation
- Choose an index
iwhere0 <= i < n - 1. - Left segment:
nums[0..i] - Right segment:
nums[i+1..n-1]
Output Format
Output
- Return the smallest valid split index
i, or-1if no valid split exists.
Constraints
2 <= nnums[i]are integers- The answer, if it exists, is an index in
[0, n-2]
Time/space constraints are not specified here; solve efficiently for large arrays.
Example 1
Input
nums = [1,2,2,2,3,2]
Output
1
Explanation
Split at index 1 gives left [1,2] and right [2,2,3,2]. The value 2 appears 1/2 times on the left? No, so this split is invalid. The minimum valid split is actually index 2: left [1,2,2] has 2 appearing 2/3 times, and right [2,3,2] has 2 appearing 2/3 times. So the answer is 2.
Example 2
Input
nums = [1,1,1,1]
Output
0
Explanation
Split at index 0 gives left [1] and right [1,1,1]. The value 1 dominates both parts, so the minimum valid split is 0.
Show 1 more example
Example 3
Input
nums = [1,2,3,4]
Output
-1
Explanation
No value can dominate both parts for any split.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.