![]() |
VOOZH | about |
Given an array of n integers. Write a program to find a minimum number of changes in the array so that the array is strictly increasing of integers. 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 change a[2] to any value between 2 and 5 and a[4] to any value greater than 5.Input: arr[] = { 1, 2, 3, 5, 7, 11 }
Output : 0
An 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.
Implementation:
2
Time Complexity:O(n*n), as nested loops are used
Auxiliary Space: O(n), Use of an array to store LIS values at each index.