Increase Values Just Enough for Strict Order

Quick Overview

Increase Values Just Enough for Strict Order. One operation increments one entry of nums by one. Without rearranging or decreasing entries, make the array strictly increasing and return the smallest number of operations required.

Increase Values Just Enough for Strict Order

Company: Infosys

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem One operation increments one entry of `nums` by one. Without rearranging or decreasing entries, make the array strictly increasing and return the smallest number of operations required. ```cpp class Solution { public: int minOperations(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text nums = [3,3,2] Output: 4 Explanation: The minimal final values are [3,4,5], costing 0+1+3. ``` **Example 2:** ```text nums = [2,5,7] Output: 0 Explanation: The array already strictly increases. ``` ### Constraints - `1 <= nums.length <= 5000`. - Original values are in `[1,10^4]`. ### Clarifying Questions The output is an operation count. Final values may exceed the original input bound because increments are unrestricted. ```hint Inspect the structure Which changes are allowed, and must the result be strictly increasing rather than merely nondecreasing? ``` ### Reasoning checkpoints - Use only increments and keep the original entry order. - Prove that the returned total is the minimum cost of making every adjacent pair strictly increasing. ### Optional extensions How would allowing decrements as well change the optimization?

Overview: Increase Values Just Enough for Strict Order. One operation increments one entry of nums by one. Without rearranging or decreasing entries, make the array strictly increasing and return the smallest number of operations required.

Infosys logo
Infosys
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

One operation increments one entry of nums by one. Without rearranging or decreasing entries, make the array strictly increasing and return the smallest number of operations required.

class Solution {
public:
    int minOperations(vector<int>& nums);
};

Examples

Example 1:

nums = [3,3,2]
Output: 4
Explanation: The minimal final values are [3,4,5], costing 0+1+3.

Example 2:

nums = [2,5,7]
Output: 0
Explanation: The array already strictly increases.

Constraints

  • 1 <= nums.length <= 5000 .
  • Original values are in [1,10^4] .

Clarifying Questions Guidance

The output is an operation count. Final values may exceed the original input bound because increments are unrestricted.

Reasoning checkpoints

  • Use only increments and keep the original entry order.
  • Prove that the returned total is the minimum cost of making every adjacent pair strictly increasing.

Optional extensions

How would allowing decrements as well change the optimization?