You are given an integer array nums of length n.
An index i (0 < i < n - 1) is special if nums[i] > nums[i - 1] and nums[i] > nums[i + 1].
You may perform operations where you choose any index i and increase nums[i] by 1.
Your goal is to:
Return an integer denoting the minimum total number of operations required.
Example 1:
Input: nums = [1,2,2]
Output: 1
Explanation:
nums = [1, 2, 2].nums[1] by 1, array becomes [1, 3, 2].[1, 3, 2] has 1 special index, which is the maximum achievable.Example 2:
Input: nums = [2,1,1,3]
Output: 2
Explanation:
nums = [2, 1, 1, 3].[2, 3, 1, 3].[2, 3, 1, 3] has 1 special index, which is the maximum achievable. Thus, the answer is 2.Example 3:
Input: nums = [5,2,1,4,3]
Output: 4
Explanation:
nums = [5, 2, 1, 4, 3].[5, 6, 1, 4, 3].[5, 6, 1, 4, 3] has 2 special indices, which is the maximum achievable. Thus, the answer is 4.
Constraints:
3 <= n <= 1051 <= nums[i] <= 109Problem Overview: You are given an array and can increase elements by any amount. The goal is to apply the minimum total increase so the number of special indices becomes as large as possible. A special index is defined by a condition involving prefix and suffix values, which means the solution depends on efficiently tracking partial sums and evaluating each position.
Approach 1: Brute Force Simulation (O(n2) time, O(1) space)
The most direct strategy checks every index and determines how much increase is required to satisfy the special condition. For each position, recompute prefix sums and suffix sums and calculate the minimal increment needed to make the index valid. This leads to repeated scans of the array, producing quadratic time complexity. The approach is easy to reason about and useful for validating logic during development, but it becomes too slow for large inputs.
Approach 2: Prefix Sum Optimization (O(n) time, O(n) space)
Instead of recomputing sums repeatedly, precompute prefix sums and suffix sums once using a prefix sum array. With these values available, you can evaluate each index in constant time. The key insight is that the cost to make an index special can be expressed as a simple difference between prefix and suffix contributions. By iterating once through the array and computing the required increase for each position, you determine which indices can become special with minimal modification. This reduces the overall complexity to linear time.
Approach 3: Greedy with Running Prefix Tracking (O(n) time, O(1) space)
A further optimization avoids storing full auxiliary arrays. Maintain a running prefix sum while tracking the remaining suffix sum as you move through the array. For each index, compute the required increase using the current prefix state and the remaining suffix contribution. This greedy evaluation works because the cost for each index is independent once the prefix and suffix totals are known. The approach combines ideas from array traversal and prefix calculations to produce an optimal linear scan.
Recommended for interviews: Start by describing the brute force approach to show you understand the definition of a special index. Then move quickly to the prefix-sum based optimization. Interviewers typically expect the O(n) scan using prefix tracking because it demonstrates familiarity with prefix sum techniques and greedy reasoning. The optimal implementation is concise and scales well for large arrays.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Simulation | O(n^2) | O(1) | Useful for understanding the condition and verifying correctness on small inputs |
| Prefix Sum Arrays | O(n) | O(n) | General solution when precomputing prefix and suffix sums simplifies evaluation |
| Greedy Prefix Tracking | O(n) | O(1) | Optimal approach when memory usage should stay minimal |
Practice Minimum Increase to Maximize Special Indices with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor