GATE CS 2024 Set 2 — Question 35
NAT+1 / -0MediumLongest Increasing SubsequenceDynamic ProgrammingAlgorithms
Algorithms → Dynamic Programming → Longest Increasing Subsequence
Last updated
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 ___________
Correct answer
3 to 3
Solution
To minimize the number of replacements required to make the array sorted in non-decreasing order, we should keep the maximum number of elements from the original array that already form a non-decreasing subsequence. The remaining elements will be replaced.Let be the length of the array. The minimum number of replacements is given by:The given array is . The length .Let's find the Longest Non-Decreasing Subsequence:
1.Start with the first element:
2.Next is 5 ():
3.Next is 3. It breaks the order with 5. We can form (replacing 5 in the active list of potential end values).
4.Next is 1. It breaks the order with 2. We can form .
5.Next is 4 ():
6.Next is 2. It breaks the order with 3. We can form .
7.Next is 6 ():
The length of the LNDS is 4. One such subsequence is (indices 0, 2, 4, 6).Therefore, the minimum number of replacements is:More questions on Dynamic Programming
2024 Set 1 Q17Given an integer array of size , we want to check if the array is sorted (in either ascending or…2024 Set 1 Q42Consider the following recurrence relation:…2024 Set 1 Q45Let be a directed graph and a depth first search (DFS) spanning tree in that is rooted…2024 Set 2 Q59The number of distinct minimum-weight spanning trees of the following graph is _________ [figure]
Practice GATE CS PYQs with adaptive difficulty
Timed practice, skill tracking, and AI explanations — free to start.
Start practicing free