I have a problem in which we have an array of positive numbers and we have to make it strictly increasing by making zero or more changes to the array elements.
We ar
This is very close to the standard longest increasing subsequence problem which is solvable in O(nlogn).
If you could change the numbers to decimals then the answer would be identical. (Min number of changes = length of string-length of longest increasing subsequence)
However, as you need distinct integral values in between you will have to slightly modify the standard algorithm.
Consider what happens if you change the array by doing x[i]=x[i]-i.
You now need to modify this changed array by making the smallest number of changes such that each element increases, or stays the same.
You can now search for the longest non-decreasing subsequence in this array and this will tell you how many elements can stay the same.
However, this may still use negative integers.
One easy way to modify the algorithm to only use positive numbers is to append a whole lot of numbers at the start of the array.
i.e. change 1,2,9,10,3,15 to -5,-4,-3,-2,-1,1,2,9,10,3,15
Then you can be sure that the optimal answer will never decide to make the 1 go negative because it would cost so much to make all the negative numbers smaller.
(You can also modify the longest increasing subsequence algorithm to have the additional constraint, but this might be harder to code correctly in an interview situation.)
Following this through on the initial example:
Original sequence
1,2,9,10,3,15
Add dummy elements at start
-5,-4,-3,-2,-1,1,2,9,10,3,15
Subtract off position in array
-5,-5,-5,-5,-5,-4,-4,2,2,-6,5
Find longest non-decreasing sequence
-5,-5,-5,-5,-5,-4,-4,2,2,*,5
So answer is to change one number.
Original sequence
1,2,2,2,3,4,5
Add dummy elements at start
-5,-4,-3,-2,-1,1,2,2,2,3,4,5
Subtract off position in array
-5,-5,-5,-5,-5,-4,-4,-5,-6,-6,-6,-6
Find longest non-decreasing sequence
-5,-5,-5,-5,-5,-4,-4,*,*,*,*,*
So answer is to change 5 numbers.