You are given an integer array nums of length n and an integer k.
For each index i, define its instability score as max(nums[0..i]) - min(nums[i..n - 1]).
In other words:
max(nums[0..i]) is the largest value among the elements from index 0 to index i.min(nums[i..n - 1]) is the smallest value among the elements from index i to index n - 1.An index i is called stable if its instability score is less than or equal to k.
Return the smallest stable index. If no such index exists, return -1.
Example 1:
Input: nums = [5,0,1,4], k = 3
Output: 3
Explanation:
[5] is 5, and the minimum in [5, 0, 1, 4] is 0, so the instability score is 5 - 0 = 5.[5, 0] is 5, and the minimum in [0, 1, 4] is 0, so the instability score is 5 - 0 = 5.[5, 0, 1] is 5, and the minimum in [1, 4] is 1, so the instability score is 5 - 1 = 4.[5, 0, 1, 4] is 5, and the minimum in [4] is 4, so the instability score is 5 - 4 = 1.k = 3. Thus, the answer is 3.Example 2:
Input: nums = [3,2,1], k = 1
Output: -1
Explanation:
3 - 1 = 2.3 - 1 = 2.3 - 1 = 2.k = 1, so the answer is -1.Example 3:
Input: nums = [0], k = 0
Output: 0
Explanation:
At index 0, the instability score is 0 - 0 = 0, which is less than or equal to k = 0. Therefore, the answer is 0.
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 1090 <= k <= 109Problem Overview: You are given an integer array and need to return the smallest index where the sum of elements to the left of that index equals the sum of elements to the right. If multiple indices satisfy the condition, the smallest index is returned. If none exist, return -1.
Approach 1: Brute Force Scan (O(n^2) time, O(1) space)
Check every index and recompute the left and right sums from scratch. For index i, iterate from 0..i-1 to compute the left sum, then iterate from i+1..n-1 to compute the right sum. If the two sums match, return that index immediately because the problem asks for the smallest valid index. This approach is straightforward and requires no extra data structures, but repeated summation makes it quadratic. It works for small arrays but becomes slow when n grows.
Approach 2: Prefix Sum Array (O(n) time, O(n) space)
Precompute cumulative sums so each range sum can be answered in constant time. Build a prefix array where prefix[i] stores the sum of elements from 0..i. The left sum for index i becomes prefix[i-1], and the right sum becomes prefix[n-1] - prefix[i]. Iterate through the array once and compare both values for each index. This reduces the repeated summation cost and guarantees linear time. The technique is a common pattern when working with array range queries.
Approach 3: Running Prefix Sum (Optimal) (O(n) time, O(1) space)
You do not actually need the full prefix array. First compute the total sum of the array. Then iterate from left to right while maintaining a running leftSum. For each index i, the right side is simply totalSum - leftSum - nums[i]. If leftSum == rightSum, return i. Otherwise update leftSum += nums[i] and continue. This approach performs a single pass and uses constant extra memory. It is the standard optimal pattern for balance-style index problems in prefix sum and array problems.
Recommended for interviews: The running prefix sum approach. Interviewers expect you to recognize that recomputing sums repeatedly is inefficient and that maintaining a running total avoids extra work. Mentioning the brute force approach first shows problem understanding, while deriving the O(n) solution demonstrates algorithmic optimization skills.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Recalculation | O(n^2) | O(1) | Useful for understanding the definition of the stable index or when constraints are very small |
| Prefix Sum Array | O(n) | O(n) | When many range-sum queries are needed or clarity is preferred over minimal memory |
| Running Prefix Sum (Optimal) | O(n) | O(1) | Best general solution for interviews and large inputs |
Practice Smallest Stable Index I with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor