Find the smallest zero-based index whose inclusive prefix maximum minus its inclusive suffix minimum is within a threshold, for arrays of at most 100 elements.
# Find the Smallest Stable Index I
You are given an integer array `nums` of length `n` and an integer `k`.
For each zero-based index `i`, define its instability score as:
`max(nums[0..i]) - min(nums[i..n - 1])`.
The first range contains every element from index `0` through `i`, and the second contains every element from `i` through `n - 1`. Both ranges include `nums[i]`.
An index is stable when its score is at most `k`. Return the smallest stable index, or `-1` if no index is stable.
### Function
`int firstStableIndex(vector<int>& nums, int k)`
### Example 1
**Input:** `nums = [8, 2, 5, 7], k = 3`
**Output:** `2`
**Explanation:** The instability scores at indices `0`, `1`, `2` and `3` are `6`, `6`, `3` and `1`. Index `2` is the first whose score is at most `3`.
### Example 2
**Input:** `nums = [9, 3, 1], k = 7`
**Output:** `-1`
**Explanation:** Every prefix maximum is `9` and every suffix minimum is `1`, so all three scores equal `8`. None is at most `7`.
### Constraints
- `1 <= nums.length <= 100`
- `0 <= nums[i] <= 1000000000`
- `0 <= k <= 1000000000`
### Clarifications
- Indices are zero-based, and a score equal to `k` qualifies.
- Values may repeat.
- A one-element array has score `0` at its only index.
### What a Strong Answer Covers
- Including the current index in both ranges.
- Returning the first qualifying index rather than an arbitrary qualifying index.
- Handling equality and the case where no index qualifies.
Overview: Find the smallest zero-based index whose inclusive prefix maximum minus its inclusive suffix minimum is within a threshold, for arrays of at most 100 elements.
You are given an integer array nums of length n and an integer k.
For each zero-based index i, define its instability score as:
max(nums[0..i]) - min(nums[i..n - 1]).
The first range contains every element from index 0 through i, and the second contains every element from i through n - 1. Both ranges include nums[i].
An index is stable when its score is at most k. Return the smallest stable index, or -1 if no index is stable.
Function
int firstStableIndex(vector<int>& nums, int k)
Example 1
Input:nums = [8, 2, 5, 7], k = 3
Output:2
Explanation: The instability scores at indices 0, 1, 2 and 3 are 6, 6, 3 and 1. Index 2 is the first whose score is at most 3.
Example 2
Input:nums = [9, 3, 1], k = 7
Output:-1
Explanation: Every prefix maximum is 9 and every suffix minimum is 1, so all three scores equal 8. None is at most 7.
Constraints
1 <= nums.length <= 100
0 <= nums[i] <= 1000000000
0 <= k <= 1000000000
Clarifications
Indices are zero-based, and a score equal to
k
qualifies.
Values may repeat.
A one-element array has score
0
at its only index.
What a Strong Answer Covers Guidance
Including the current index in both ranges.
Returning the first qualifying index rather than an arbitrary qualifying index.
Handling equality and the case where no index qualifies.