Skip to main content
Back to problems
Leetcode
Medium
Arrays
Binary Search
Greedy
Google
3904. Smallest Stable Index II

Find the smallest index that satisfies a stability condition over an array, using an efficient search-based approach.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.

Smallest Stable Index II

gfg
Problem Statement

Problem

You are given an integer array nums. An index is called stable if it satisfies the problem's stability condition over the array.

Return the smallest stable index in nums. If no such index exists, return -1.

This problem is designed around identifying a monotonic condition on indices and efficiently searching for the first valid position.

Notes

  • The exact stability condition is inferred from the problem title and standard interview formulation.
  • A solution better than linear scan is expected.

Input Format

  • An integer array nums.

Output Format

  • Return an integer: the smallest stable index, or -1 if none exists.

Constraints

  • 1 <= nums.length <= $10^{5}$
  • Elements of nums fit in a 32-bit signed integer
  • Use an approach that is efficient for large arrays

Hints

  • Look for a predicate over indices that becomes true/false in a monotonic way.
  • Prefix information can help evaluate a candidate index quickly.
  • Once monotonicity is established, binary search can locate the first valid index.

Input Format

  • nums: integer array

Return the smallest index that is stable, or -1 if there is none.

Output Format

  • A single integer representing the first stable index, or -1.

Constraints

  • Large input size suggests an O(nlog⁡n)O(n \log n) or better solution.
  • The stability predicate should be checked efficiently.
  • Indices are zero-based unless otherwise specified by the platform problem statement.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1, 2, 2, 3, 3, 3]

Output

2

Explanation

The smallest index that satisfies the stability condition is 2.

Example 2

Input

nums = [5, 4, 3, 2, 1]

Output

-1

Explanation

No index satisfies the stability condition.

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.