Question:

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 ...........\_\_\_.

Show Hint

The distance of an array is calculated as the total number of elements minus the length of its Longest Increasing Subsequence (LIS).
Updated On: Jan 23, 2025
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

To calculate the distance, we need to determine the Longest Increasing Subsequence (LIS) of the array. The length of the LIS represents the largest subset of elements already in sorted order.
The given array is \([2, 5, 3, 1, 4, 2, 6]\). The LIS for this array is: \[ [2, 3, 4, 6] \quad \text{(length = 4)}. \] The minimum number of elements to replace is given by: \[ \text{Distance} = \text{Length of array} - \text{Length of LIS} = 7 - 4 = 3. \] Final Answer: \[ \boxed{3} \]
Was this answer helpful?
0
0

Top Questions on Syntax Directed Translation

Questions Asked in GATE CS exam

View More Questions