Question
Let be an array containing integer values. The distance of is defined as the minimum number of elements in that must be replaced with another integer so that the resulting array is sorted in non-decreasing order. The distance of the array is ___________
Official answer and solution
- Official answer
- 3
- From
- Official GATE 2024 answer key
Solution
Find the length of the longest non-decreasing subsequence of and subtract from the array length .
To minimize the number of elements replaced to make sorted in non-decreasing order, we maximize the number of elements kept unchanged, which must form a non-decreasing subsequence of .
Inspecting :
- The longest non-decreasing subsequence has length : specifically (at indices ).
- Keeping fixed, we can replace the remaining elements () with integers respectively to obtain the non-decreasing array .
- No non-decreasing subsequence of length exists.
Therefore, the distance of the array is .
3
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2024 Computer Science and Information Technology - Set 2 (CS2)
- Question
- 35 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.