Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Google
Minimum Index Of A Valid Split

Find the smallest split point where the same dominant value remains dominant in both parts of the array.

Acceptance 0%
Problem Statement

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 nums of length n.

Interpretation

  • Choose an index i where 0 <= 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 -1 if no valid split exists.

Constraints

  • 2 <= n
  • nums[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.

Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.