Convert to Strictly increasing integer array with minimum changes in C++


In this tutorial, we will be discussing a program to convert to strictly increasing integer array with minimum changes.

For this we will be provided with an array. Our task is to change the elements of the array to be in strictly increasing order by minimum number of changes in the elements.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//calculating number of changes required
int remove_min(int arr[], int n){
   int LIS[n], len = 0;
   for (int i = 0; i < n; i++)
   LIS[i] = 1;
   for (int i = 1; i < n; i++) {
      for (int j = 0; j < i; j++) {
         if (arr[i] > arr[j] && (i-j)<=(arr[i]-arr[j])){
            LIS[i] = max(LIS[i], LIS[j] + 1);
         }
      }
         len = max(len, LIS[i]);
   }
   //returning the changes required
   return n - len;
}
int main(){
   int arr[] = { 1, 2, 6, 5, 4 };
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << remove_min(arr, n);
   return 0;
}

Output

2

Updated on: 29-Jan-2020

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements