Convert to Strictly increasing integer array with minimum changes - GeeksforGeeks

20 Apr, 2022
Given an array of n integers. Write a program to find minimum number of changes in array so that array is strictly increasing of integers. In strictly increasing array A[i] < A[i+1] for 0 <= i < nExamples:  
Input : arr[] = { 1, 2, 6, 5, 4}
Output : 2
We can change a[2] to any value 
between 2 and 5.
and a[4] to any value greater then 5. 

Input : arr[] = { 1, 2, 3, 5, 7, 11 }
Output : 0
Array is already strictly increasing.
 
The problem is variation of Longest Increasing Subsequence. The numbers which are already a part of LIS need not to be changed. So minimum elements to change is difference of size of array and number of elements in LIS. Note that we also need to make sure that the numbers are integers. So while making LIS, we do not consider those elements as part of LIS that cannot form strictly increasing by inserting elements in middle. Example { 1, 2, 5, 3, 4 }, we consider length of LIS as three {1, 2, 5}, not as {1, 2, 3, 4} because we cannot make a strictly increasing array of integers with this LIS.  
C++
C
Java
Python3
C#
PHP
Javascript
// CPP program to find min elements to// change so array is strictly increasing#include <bits/stdc++.h>using namespace std; // To find min elements to remove from array// to make it strictly increasingint minRemove(int arr[], int n){    int LIS[n], len = 0;     // Mark all elements of LIS as 1    for (int i = 0; i < n; i++)        LIS[i] = 1;     // Find LIS of array    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]);    }     // Return min changes for array to strictly increasing    return n - len;} // Driver program to test minRemove()int main(){    int arr[] = { 1, 2, 6, 5, 4 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << minRemove(arr, n);    return 0;} // This code is contributed by Sania Kumari Gupta
// C program to find min elements to// change so array is strictly increasing#include <stdio.h> // Find maximum between two numbers.int max(int num1, int num2){    return (num1 > num2) ? num1 : num2;} // To find min elements to remove from array// to make it strictly increasingint minRemove(int arr[], int n){    int LIS[n], len = 0;     // Mark all elements of LIS as 1    for (int i = 0; i < n; i++)        LIS[i] = 1;     // Find LIS of array    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]);    }     // Return min changes for array to strictly increasing    return n - len;} // Driver program to test minRemove()int main(){    int arr[] = { 1, 2, 6, 5, 4 };    int n = sizeof(arr) / sizeof(arr[0]);    printf("%d", minRemove(arr, n));    return 0;} // This code is contributed by Sania Kumari Gupta
// Java program to find min elements to// change so array is strictly increasingpublic class Main {     // To find min elements to remove from array    // to make it strictly increasing    static int minRemove(int arr[], int n)    {        int LIS[] = new int[n];        int len = 0;         // Mark all elements of LIS as 1        for (int i = 0; i < n; i++)            LIS[i] = 1;         // Find LIS of array        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] = Math.max(LIS[i], LIS[j] + 1);            }            len = Math.max(len, LIS[i]);        }         // Return min changes for array to strictly        // increasing        return n - len;    }     // Driver program to test minRemove()    public static void main(String[] args)    {        int arr[] = { 1, 2, 6, 5, 4 };        int n = arr.length;        System.out.println(minRemove(arr, n));    }} // This code is contributed by Sania Kumari Gupta
# Python3 program to find min elements to# change so array is strictly increasing # Find min elements to remove from array# to make it strictly increasingdef minRemove(arr, n):    LIS = [0 for i in range(n)]    len = 0     # Mark all elements of LIS as 1    for i in range(n):        LIS[i] = 1     # Find LIS of array    for i in range(1, n):                 for j in range(i):            if (arr[i] > arr[j] and (i-j)<=(arr[i]-arr[j]) ):                LIS[i] = max(LIS[i], LIS[j] + 1)                         len = max(len, LIS[i])     # Return min changes for array    # to strictly increasing    return (n - len) # Driver Codearr = [ 1, 2, 6, 5, 4 ]n = len(arr)print(minRemove(arr, n)) # This code is contributed by Azkia Anam.
// C# program to find min elements to change so// array is strictly increasingusing System; class GFG{     // To find min elements to remove from array to    // make it strictly increasing    static int minRemove(int []arr,                        int n)    {        int []LIS = new int[n];        int len = 0;         // Mark all elements        // of LIS as 1        for (int i = 0; i < n; i++)            LIS[i] = 1;         // Find LIS of array        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] = Math.Max(LIS[i],                                LIS[j] + 1);            }            len = Math.Max(len, LIS[i]);        }         // Return min changes for array         // to strictly increasing        return n - len;    }     // Driver Code    public static void Main()    {        int []arr = {1, 2, 6, 5, 4};        int n = arr.Length;         Console.WriteLine(minRemove(arr, n));    }} // This code is contributed// by anuj_67.
<?php// PHP program to find min elements to change so// array is strictly increasing // To find min elements to remove from array // to make it strictly increasingfunction minRemove($arr, $n){    $LIS = array();    $len = 0;     // Mark all elements    // of LIS as 1    for ($i = 0; $i < $n; $i++)        $LIS[$i] = 1;     // Find LIS of array    for ($i = 1; $i < $n; $i++)    {        for ($j = 0; $j < $i; $j++)        {            if ($arr[$i] > $arr[$j])                $LIS[$i] = max($LIS[$i],                            $LIS[$j] + 1);        }        $len = max($len, $LIS[$i]);    }     // Return min changes for array to strictly    // increasing    return $n - $len;} // Driver Code$arr = array(1, 2, 6, 5, 4);$n = count($arr); echo minRemove($arr, $n); // This code is contributed// by anuj_6?>
<script> // Javascript program to find min elements to// change so array is strictly increasing     // To find min elements to remove from array    // to make it strictly increasing    function minRemove(arr, n)    {        let LIS = new Array(n).fill(0);        let len = 0;           // Mark all elements of LIS as 1        for (let i = 0; i < n; i++)            LIS[i] = 1;           // Find LIS of array        for (let i = 1; i < n; i++) {            for (let j = 0; j < i; j++) {                if (arr[i] > arr[j] && (i-j)<=(arr[i]-arr[j]))                    LIS[i] = Math.max(LIS[i],                                 LIS[j] + 1);            }            len = Math.max(len, LIS[i]);        }           // Return min changes for array        // to strictly increasing        return n - len;    }     // driver program             let arr = [ 1, 2, 6, 5, 4 ];        let n = arr.length;           document.write(minRemove(arr, n)); // This code is contributed by Code_hunt.</script>
Output:  
2
This article is contributed by nuclode. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
surya jeet singh
code_hunt
krisania804
LIS
Arrays
Dynamic Programming
Arrays
Dynamic Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
0-1 Knapsack Problem | DP-10
Program for Fibonacci numbers
Longest Common Subsequence | DP-4
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16