Q43.Marks: +2.0UGC NET Paper 2: Computer Science and Application 26th June 2025 Shift 1
The longest common subsequence of {1,2,3,2,4,1,2} and {2,4,3,1,2,1} is
1.2,1,2,3
2.1,3,2,1
3.2,3,2,1✓ Correct
4.2,3,1,2,1
Solution
The correct answer is Option 3) 2,3,2,1
Key Points
LCS (Longest Common Subsequence) is a sequence that appears in both lists in the same order (not necessarily contiguously) and has maximum possible length.
Let A = {1,2,3,2,4,1,2} and B = {2,4,3,1,2,1}. We will use the standard DP (dynamic programming) table of lengths L[i][j] = LCS length of A[1..i] and B[1..j].
DP Length Table (final values)
i\j
1(2)
2(4)
3(3)
4(1)
5(2)
6(1)
1(1)
0
0
0
1
1
1
2(2)
1
1
1
1
2
2
3(3)
1
1
2
2
2
2
4(2)
1
1
2
2
3
3
5(4)
1
2
2
2
3
3
6(1)
1
2
2
3
3
4
7(2)
1
2
2
3
4
4
The bottom-right value is 4, so the LCS length is 4.
Reconstruction (backtracking idea)
From the last cell (length 4), match the last common element: A[6]=1 matches B[6]=1 → include 1.
Move to subproblem before these indices: we are now at length 3 with prefixes ending before A[6] and B[6].
Trace previous matches: before the final 1, we can match 2 (A[4] with B[5]), before that 3 (A[3] with B[3]), and before that 2 (A[2] with B[1]).
Thus one LCS is {2, 3, 2, 1}.
Why other options are wrong
2,1,2,3: In B, after taking the last 2 (position 5), there is no 3 to the right, so this order cannot occur.
1,3,2,1: In B, 1 appears at positions 4 and 6; there is no 3 after position 4 that can still keep the order 1→3, so this sequence cannot occur.
2,3,1,2,1 (length 5): Not a subsequence of A (after 1 at position 6, there is no trailing 1 to complete length 5); hence impossible, and anyway longer than the DP length 4.
Conclusion: The longest common subsequence of the two sequences is 2,3,2,1 (length 4).