Minimum Operations to Make the Array K-Increasing - Problem

You are given a 0-indexed array arr consisting of n positive integers, and a positive integer k.

The array arr is called K-increasing if arr[i-k] <= arr[i] holds for every index i, where k <= i <= n-1.

For example, arr = [4, 1, 5, 2, 6, 2] is K-increasing for k = 2 because:

  • arr[0] <= arr[2] (4 <= 5)
  • arr[1] <= arr[3] (1 <= 2)
  • arr[2] <= arr[4] (5 <= 6)
  • arr[3] <= arr[5] (2 <= 2)

However, the same arr is not K-increasing for k = 1 (because arr[0] > arr[1]) or k = 3 (because arr[0] > arr[3]).

In one operation, you can choose an index i and change arr[i] into any positive integer.

Return the minimum number of operations required to make the array K-increasing for the given k.

Input & Output

Example 1 — Basic K-increasing
$ Input: arr = [4,1,5,2,6,2], k = 2
Output: 1
💡 Note: Split into subsequences: [4,5,6] (already increasing) and [1,2,2] (need to change one element to make it strictly increasing). Total operations = 1.
Example 2 — All elements need changes
$ Input: arr = [5,4,3,2,1], k = 1
Output: 4
💡 Note: For k=1, entire array must be non-decreasing. We need to change 4 elements to make [5,4,3,2,1] into something like [1,2,3,4,5].
Example 3 — Already K-increasing
$ Input: arr = [1,2,3,4], k = 2
Output: 0
💡 Note: Subsequence [1,3] and [2,4] are both already increasing, so no operations needed.

Constraints

  • 1 ≤ arr.length ≤ 105
  • 1 ≤ arr[i] ≤ 109
  • 1 ≤ k ≤ arr.length

Visualization

Tap to expand
K-Increasing Array: Minimum OperationsInput: arr = [4,1,5,2,6,2], k = 2415262012345456[4,5,6]: Already increasing, 0 ops123[1,2,2] → [1,2,3]: Change last element, 1 opTotal Operations: 0 + 1 = 1
Understanding the Visualization
1
Input Array
arr = [4,1,5,2,6,2], k = 2
2
Split into Subsequences
Group by index % k: [4,5,6] and [1,2,2]
3
Find Operations
LIS approach: 0 + 1 = 1 operation needed
Key Takeaway
🎯 Key Insight: Transform K-increasing problem into k independent LIS problems for optimal solution
Asked in
Google 25 Facebook 20 Amazon 15
12.0K Views
Medium Frequency
~35 min Avg. Time
580 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen