# Convert to Strictly increasing array with minimum changes

Given an array of n integers. Write a program to find minimum number of changes in array so that array is strictly increasing. In strictly increasing array A[i] < A[i+1] for 0 <= i < n

Examples:

```Input : arr[] = { 1, 2, 6, 5, 4}
Output : 2
We can 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
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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.

## C++

 `// CPP program to find min elements to ` `// change so array is strictly increasing ` `#include ` `using` `namespace` `std; ` ` `  `// To find min elements to remove from array ` `// to make it strictly increasing ` `int` `minRemove(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `LCS[n], len = 0; ` ` `  `    ``// Mark all elements of LCS as 1 ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``LCS[i] = 1; ` ` `  `    ``// Find LCS 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])){ ` `                ``LCS[i] = max(LCS[i], LCS[j] + 1); ` `            ``} ` `        ``} ` `        ``len = max(len, LCS[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; ` `} `

## Java

 `// Java program to find min elements to ` `// change so array is strictly increasing ` `public` `class` `Main { ` ` `  `    ``// To find min elements to remove from array ` `    ``// to make it strictly increasing ` `    ``static` `int` `minRemove(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``int` `LCS[] = ``new` `int``[n]; ` `        ``int` `len = ``0``; ` ` `  `        ``// Mark all elements of LCS as 1 ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``LCS[i] = ``1``; ` ` `  `        ``// Find LCS 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])) ` `                    ``LCS[i] = Math.max(LCS[i],  ` `                                 ``LCS[j] + ``1``); ` `            ``} ` `            ``len = Math.max(len, LCS[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)); ` `    ``} ` `} `

## Python3

 `# Python3 program to find min elements to ` `# change so array is strictly increasing ` ` `  `# Find min elements to remove from array ` `# to make it strictly increasing ` `def` `minRemove(arr, n): ` `    ``LCS ``=` `[``0` `for` `i ``in` `range``(n)] ` `    ``len` `=` `0` ` `  `    ``# Mark all elements of LCS as 1 ` `    ``for` `i ``in` `range``(n): ` `        ``LCS[i] ``=` `1` ` `  `    ``# Find LCS 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]) ): ` `                ``LCS[i] ``=` `max``(LCS[i], LCS[j] ``+` `1``) ` `                 `  `        ``len` `=` `max``(``len``, LCS[i]) ` ` `  `    ``# Return min changes for array ` `    ``# to strictly increasing ` `    ``return` `(n ``-` `len``) ` ` `  `# Driver Code ` `arr ``=` `[ ``1``, ``2``, ``6``, ``5``, ``4` `] ` `n ``=` `len``(arr) ` `print``(minRemove(arr, n)) ` ` `  `# This code is contributed by Azkia Anam. `

## C#

 `// C# program to find min elements to change so  ` `// array is strictly increasing ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// To find min elements to remove from array to  ` `    ``// make it strictly increasing ` `    ``static` `int` `minRemove(``int` `[]arr,  ` `                        ``int` `n) ` `    ``{ ` `        ``int` `[]LCS = ``new` `int``[n]; ` `        ``int` `len = 0; ` ` `  `        ``// Mark all elements ` `        ``// of LCS as 1 ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``LCS[i] = 1; ` ` `  `        ``// Find LCS 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])) ` `                    ``LCS[i] = Math.Max(LCS[i],  ` `                                ``LCS[j] + 1); ` `            ``} ` `            ``len = Math.Max(len, LCS[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

 ` ``\$arr``[``\$j``]) ` `                ``\$LCS``[``\$i``] = max(``\$LCS``[``\$i``],  ` `                            ``\$LCS``[``\$j``] + 1); ` `        ``} ` `        ``\$len` `= max(``\$len``, ``\$LCS``[``\$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 ` `?> `

Output:

```2
```

This article is contributed by nuclode. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : vt_m, surya jeet singh

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.