Find the smallest index that satisfies a stability condition over an array, using an efficient search-based approach.
Smallest Stable Index II
gfgYou 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.
nums.-1 if none exists.1 <= nums.length <= $10^{5}$nums fit in a 32-bit signed integernums: integer arrayReturn the smallest index that is stable, or -1 if there is none.
-1.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
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.