The Wayback Machine - https://web.archive.org/web/20220105223326/https://www.geeksforgeeks.org/minimum-replacements-with-any-positive-integer-to-make-the-array-k-increasing/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimum replacements with any positive integer to make the array K-increasing

  • Last Updated : 05 Jan, 2022

Given an array arr[] of N positive integers and an integer K, The task is to replace minimum number of elements with any positive integer to make the array K-increasing. An array is K-increasing if for every index i in range [K, N), arr[i] ≥ arr[i-K] 

Examples:

Resolution Days 2022 GeeksforGeeks

Input: arr[] = {4, 1, 5, 2, 6, 2}, k = 2
Output: 0
Explanation: Here, for every index i where 2 <= i <= 5, arr[i-2] <= arr[i]
Since the given array is already K-increasing, there is no need to perform any operations.

Input: arr[] = {4, 1, 5, 2, 6, 2}, k = 3
Output: 2
Explanation: Indices 3 and 5 are the only ones not satisfying arr[i-3] <= arr[i] for 3 <= i <= 5.
One of the ways we can make the array K-increasing is by changing arr[3] to 4 and arr[5] to 5.
The array will now be [4,1,5,4,6,5].



 

Approach: This solution is based on finding the longest increasing subsequence. Since the above question requires that arr[i-K] ≤ arr[i]  should hold for every index i, where K ≤ i ≤ N-1, here the importance is given to compare the elements which are K places away from each other.
So the task is to confirm that the sequences formed by the elements K places away are all non decreasing in nature. If they are not then perform the replacements to make them non-decreasing.
Follow the steps mentioned below:

  • Traverse the array and form sequence(seq[]) by picking elements K places away from each other in the given array.
  • Check if all the elements in seq[] is non-decreasing or not.
  • If not then find the length of longest non-decreasing subsequence of seq[].
  • Replace the remaining elements to minimize the total number of operations.
  • Sum of replacement operations for all such sequences is the final answer.

Follow the below illustration for better understanding.

• For example: arr[] = {4, 1, 5, 2, 6, 0, 1} ,K = 2

Indices: 0  1  2  3  4  5  6
values:  4  1  5  2  6  0 1

So the work is to ensure that following sequences

  1. arr[0], arr[2], arr[4], arr[6] => {4, 5, 6, 1}
  2. arr[1], arr[3], arr[5] => {1, 2, 0}

Obey arr[i-k] <= arr[i]
So for first sequence it can be seen that {4, 5, 6} are K increasing and it is the longest non-decreasing subsequence, whereas 1 is not, so one operation is needed for it.
Similarly, for 2nd {1, 2} are longest non-decreasing whereas 0 is not, so one operation is needed for it.

So total 2 minimum operations are required.  

Below is the implementation of the above approach. 

C++




// C++ code to implement above approach
#include <bits/stdc++.h>
using namespace std;
  
// Functions finds the 
// longest non decreasing subsequence.
int utility(vector<int>& arr, int& n)
{
    vector<int> tail;
    int len = 1;
    tail.push_back(arr[0]);
    for (int i = 1; i < n; i++) {
        if (tail[len - 1] <= arr[i]) {
            len++;
            tail.push_back(arr[i]);
        }
        else {
            auto it = upper_bound(tail.begin(), 
                                  tail.end(),
                                  arr[i]);
            *it = arr[i];
        }
    }
    return len;
}
  
// Function to find the minimum operations
// to make array K-increasing
int kIncreasing(vector<int>& a, int K)
{
    int ans = 0;
      
    // Size of array
    int N = a.size();
    for (int i = 0; i < K; i++)
    {
        // Consider all elements K-places away
        // as a sequence
        vector<int> v;
        
        for (int j = i; j < N; j += K)
        {
            v.push_back(a[j]);
        }
          
        // Size of each sequence
        int k = v.size();
         
        // Store least operations
        // for this sequence
        ans += k - utility(v, k); 
    }
    return ans;
}
  
// Driver code
int main()
{
    vector<int> arr{ 4, 1, 5, 2, 6, 0, 1 };
    int K = 2;
    cout << kIncreasing(arr, K);
    return 0;
}
Output
2

Time Complexity: O(K * N * logN)
Auxiliary Space: O(N)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!