Skip to main content

Smallest Stable Index II - Video Solutions

MediumArray

Smallest Stable Index I & II | LeetCode 3903 | LeetCode 3904 | Weekly Contest 498 | Developer Coder

Developer Coder
9:56165 views
10 video solutions available

Smallest Stable Index II - Video Solution

Watch 10 video solutions for Smallest Stable Index II, a medium level problem involving Array. This walkthrough by Developer Coder has 165 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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:

  • At index 0: The maximum in [5] is 5, and the minimum in [5, 0, 1, 4] is 0, so the instability score is 5 - 0 = 5.
  • At index 1: The maximum in [5, 0] is 5, and the minimum in [0, 1, 4] is 0, so the instability score is 5 - 0 = 5.
  • At index 2: The maximum in [5, 0, 1] is 5, and the minimum in [1, 4] is 1, so the instability score is 5 - 1 = 4.
  • At index 3: The maximum in [5, 0, 1, 4] is 5, and the minimum in [4] is 4, so the instability score is 5 - 4 = 1.
  • This is the first index with an instability score less than or equal to k = 3. Thus, the answer is 3.

Example 2:

Input: nums = [3,2,1], k = 1

Output: -1

Explanation:

  • At index 0, the instability score is 3 - 1 = 2.
  • At index 1, the instability score is 3 - 1 = 2.
  • At index 2, the instability score is 3 - 1 = 2.
  • None of these values is less than or equal to 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 <= 105
  • 0 <= nums[i] <= 109
  • 0 <= k <= 109
Read full problem with examples

Approach Overview

Problem Overview: Given an integer array, a stable index is an index where the sum of elements on the left equals the sum of elements on the right. The task is to return the smallest such index. If no index satisfies the condition, return -1.

Approach 1: Brute Force Scan (O(n^2) time, O(1) space)

Iterate through every index i and recompute the left and right sums each time. For each position, loop from 0..i-1 to compute the left sum and from i+1..n-1 to compute the right sum. If the two sums match, return the index immediately since the goal is the smallest valid index. This approach uses constant extra memory but performs repeated work, leading to quadratic time.

Approach 2: Prefix Sum Array (O(n) time, O(n) space)

Precompute a prefix sum 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 once through the array and compare both sums. Each lookup becomes constant time, reducing overall complexity to linear time while using extra memory for the prefix array. This technique is common when solving problems involving cumulative ranges in arrays and prefix sums.

Approach 3: Running Prefix Sum (Optimal) (O(n) time, O(1) space)

First compute the total sum of the array. Then iterate through the array while maintaining a running leftSum. For index i, the right sum is simply totalSum - leftSum - nums[i]. Compare leftSum and the computed right sum. If they match, return i. After the check, add nums[i] to leftSum and continue. This eliminates the need for an auxiliary prefix array while preserving linear performance.

Recommended for interviews: The running prefix sum approach is the expected solution. Brute force shows the baseline reasoning, but interviewers usually want to see the observation that the right side can be derived from the total sum and a running left sum. That insight reduces repeated work and demonstrates strong understanding of array traversal and cumulative sum techniques.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force ScanO(n^2)O(1)Good for initial reasoning or very small arrays
Prefix Sum ArrayO(n)O(n)Useful when prefix sums are reused for multiple queries
Running Prefix Sum (Optimal)O(n)O(1)Best general solution with minimal memory usage