GATE / CS ARCHIVE

GATE CSE 2024 SET 2 / QUESTION 35

Minimum replacements via a longest nondecreasing subsequence

Algorithms · Dynamic Programming · Full GATE CSE 2024 Set 2 paper

GATE 2024 Set 2, Q35 1 mark · NAT
Report issue

Dynamic Programming

Question 35, GATE 2024: The distance of an integer array is the least number of entries replaced by arbitrary integers to make it nondecreasing. Find the distance of [2,5,3,1,4,2,6].
Tap to enlarge
Answer & solution

Official answer: 3

Step through it (9 steps)

Approach: Any entries kept in place must already form a nondecreasing subsequence; combine that lower bound with an explicit repair.

1. Derivation. Any unchanged elements must form a nondecreasing subsequence. Longest nondecreasing subsequence lengths ending at the seven positions are 1, 2, 2, 1, 3, 2, 4, so at most 4 elements can remain unchanged and at least 7−4=3 replacements are needed. Keep the subsequence 2, 3, 4, 6 at positions 1, 3, 5, 7; replace positions 2, 4, 6 by 2, 3, 4 respectively to obtain[2, 2, 3, 3, 4, 4, 6]. Thus3 is achievable and minimal.

2. Explanation and checks. For each position i define L[i]=1+max L[j] over j<i with A[j]≤A[i], or 1 if there is no such j. This yields [1,2,2,1,3,2,4], with maximum 4. Therefore keeping five entries is impossible. The displayed repair keeps four original entries and changes exactly three, establishing the matching upper bound. It is a replacement problem, not a sorting-by-swaps problem, and equality is allowed in the final array.

Original paper, page 23 · Answer key