GateAI
Dynamic Programming

GATE CSE 2024 Set 2 · Q35

NAT1 markMedium

Time

Let AA be an array containing integer values. The distance of AA is defined as the minimum number of elements in AA that must be replaced with another integer so that the resulting array is sorted in non-decreasing order. The distance of the array [2,5,3,1,4,2,6][2, 5, 3, 1, 4, 2, 6] is _________

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 3

Dynamic Programming

To minimize the number of replacements needed to make the array sorted in non-decreasing order, we should maximize the number of elements that remain unchanged.

Since we can replace elements with any integer (and the condition is non-decreasing, i.e., x1≤x2≤⋯≤xnx_1 \le x_2 \le \dots \le x_n), the elements that remain unchanged must form a longest non-decreasing subsequence (LNDS) of AA.

Distance(A)=n−Length of LNDS(A)\text{Distance}(A) = n - \text{Length of LNDS}(A)

Given the array:

A=[2,5,3,1,4,2,6]A = [2, 5, 3, 1, 4, 2, 6]

where n=7n = 7.

Let us find the length of the longest non-decreasing subsequence:

  • A valid non-decreasing subsequence is [2,3,4,6][2, 3, 4, 6] (indices 0,2,4,60, 2, 4, 6), which has length 44.

  • No non-decreasing subsequence of length 55 or more exists.

Therefore:

Length of LNDS=4\text{Length of LNDS} = 4
Distance(A)=7−4=3\text{Distance}(A) = 7 - 4 = 3

For instance, replacing the elements at index 11 (value 55), index 33 (value 11), and index 55 (value 22) with 2,3,42, 3, 4 respectively gives the non-decreasing array [2,2,3,3,4,4,6][2, 2, 3, 3, 4, 4, 6].

Thus, the distance of the array is 3.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Dynamic Programming previous year question is Q35 of the GATE CSE 2024 Set 2 paper. Find more GATE CSE PYQs on Dynamic Programming in Algorithms, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Dynamic Programming PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Dynamic Programming in GATE CSE

Dynamic Programming has 7 GATE CSE PYQs across 6 papers from 2011 to 2026. This is the only 1-mark NAT question among them, and it is the only one in GATE CSE 2024 Set 2.

How GATE tests Dynamic Programming

More from GATE CSE 2024 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2024 Set 2 · Q35 · NAT

Let A be an array containing integer values. The distance of A is defined as the minimum number of elements in A that must be replaced with another integer so that the resulting array is sorted in non-decreasing order. The distance of the array [2, 5, 3, 1, 4, 2, 6] is …

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.