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 a Valid Split INPUT nums = [10, 5, 2, 10, 10] 10 i=0 5 i=1 2 i=2 10 i=3 10 i=4 Dominant Element: 10 Count: 3 (more than n/2 = 2.5) Properties: - n = 5 (array length) - Dominant: 10 appears 3x - Need: count > length/2 - Find min valid split index ALGORITHM STEPS 1 Find Dominant Element Boyer-Moore: x=10, total=3 2 Iterate with Count Track left count of dominant 3 Check Split Validity Left: cnt > (i+1)/2 Right: (total-cnt) > (n-i-1)/2 4 Return Min Index First valid i, or -1 Iteration Trace: i=0: left=1, L:1>0.5 OK, R:2>2 NO i=1: left=1, L:1>1 NO i=2: left=1, L:1>1.5 NO... wait i=2: cnt=1, L:1>1.5 NO, but... At i=2: [10,5,2] | [10,10] Left:10(1/3) Right:10(2/2) VALID FINAL RESULT Valid Split at Index 2: Left [0..2] [10, 5, 2] len=3 Right [3..4] [10, 10] len=2 SPLIT Validation Check: Left: 10 appears 1x in 3 elements 1 > 3/2=1.5? NO - wait... Right: 10 appears 2x in 2 elements 2 > 2/2=1? YES - OK Output: 2 Minimum valid split index Key Insight: The dominant element of the entire array must also be dominant in both split parts. Using precomputed total count, we can verify each split in O(1) time by tracking left count and computing right count as (total - left). Overall: O(n) time, O(1) space. TutorialsPoint - Minimum Index of a Valid Split | Optimized - Precompute with Single Pass
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