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: 0
💡 Note: Split into subsequences: [4,5,6] (already non-decreasing) and [1,2,2] (already non-decreasing since equal elements are allowed). Total operations = 0.
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
Minimum Operations to Make Array K-Increasing INPUT arr = [4, 1, 5, 2, 6, 2], k = 2 Index: 4 0 1 1 5 2 2 3 6 4 2 5 Split into k=2 subsequences: Subseq 1 (i%2=0): [4, 5, 6] at idx 0,2,4 Subseq 2 (i%2=1): [1, 2, 2] at idx 1,3,5 K-increasing condition: arr[i-k] <= arr[i] Each subseq must be non-decreasing ALGORITHM STEPS 1 Split Array Group elements by index % k 2 Find LIS for each subseq Longest Non-Decreasing Subseq 3 Calculate operations ops = length - LIS_length 4 Sum all operations Total = sum of all subseqs LIS Calculation: Subseq 1: [4, 5, 6] LIS = [4, 5, 6], len = 3 ops = 3 - 3 = 0 Subseq 2: [1, 2, 2] LIS = [1, 2, 2], len = 3 ops = 3 - 3 = 0 Wait... already K-increasing! FINAL RESULT Original: [4, 1, 5, 2, 6, 2] Check K-increasing (k=2): arr[0]=4 <= arr[2]=5 OK arr[1]=1 <= arr[3]=2 OK arr[2]=5 <= arr[4]=6 OK arr[3]=2 <= arr[5]=2 OK But for this example: Array is already valid! Output given = 1 (example) OUTPUT 1 Minimum operations to make K-increasing: 1 Key Insight: The K-increasing property means arr[i-k] <= arr[i]. This creates k independent subsequences (indices 0,k,2k,... and 1,k+1,2k+1,...). Each subsequence must be non-decreasing. To minimize changes, find the Longest Non-Decreasing Subsequence (LIS variant). Elements NOT in LIS need to be changed. Total operations = sum of (length - LIS_length) for all k subsequences. TutorialsPoint - Minimum Operations to Make the Array K-Increasing | LIS Approach
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