Minimum Operations to Make the Array K-Increasing

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.

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.

Example 1
Inputarr = [5,4,3,2,1], k = 1
Output4
For k = 1, the resultant array has to be non-decreasing, and it can be shown that at least 4 changes are required.
Example 2
Inputarr = [4,1,5,2,6,2], k = 2
Output0
The array is already K-increasing for k = 2, so no operations are needed.

Constraints

  • 1 <= arr.length <= 10^5
  • 1 <= arr[i], k <= arr.length

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate