View worked solution
Answer: 3
To find the minimum number of elements that must be replaced to make the array sorted in non-decreasing order, we should keep as many elements in their original relative order as possible. These kept elements must form a Longest Non-Decreasing Subsequence (LNDS) of the array.
If is the length of the array and is the length of its LNDS, then the minimum number of replacements (distance) is given by:
For the given array of size :
- Let's find the LNDS of .
- One such longest non-decreasing subsequence is (corresponding to indices ).
- The length of this subsequence is .
Thus, the minimum number of elements to replace is:
For example, by replacing the elements at indices and (which are and ), we can obtain the sorted array with exactly replacements.
Free practice · Your answers on this page are not saved.
