• edited by
28,958 views
34 34 votes
Let $\text{A}$ be an array containing integer values. The distance of $\text{A}$ is defined as the minimum number of elements in $\text{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 ___________.

5 Answers

72 72 votes

To calculate the distance of the array, we need to determine how many elements must be replaced to transform the given array into a sorted, non-decreasing array.

Here’s how we can approach the problem:

1. Find the Longest Increasing Subsequence (LIS): The LIS will represent the largest subset of elements that are already in the correct order.
2. Calculate the distance: The distance will be the number of elements that are not part of the LIS, since those elements would need to be replaced.

Let's apply this to the given array: [2, 5, 3, 1, 4, 2, 6]

Step 1: Find the LIS
The longest increasing subsequence in this array is [2, 3, 4, 6], which has a length of 4.

Step 2: Calculate the distance
The length of the original array is 7. The number of elements that are not part of the LIS is:


Distance = 7 – 4 = 3

Thus, the minimum number of elements that must be replaced to sort the array in non-decreasing order is 3.

70 70 votes

Idea:
The question asks for the minimum number of elements that must be replaced so that the array becomes sorted in non-decreasing order.

Do not confuse this with swapping — we are not rearranging positions of elements. We are only changing their values (replacing them) so that the array becomes sorted.


If you can find the Longest Non-Decreasing Subsequence (LNDS) of the array, then those elements can stay as they are — and the rest must be replaced.

\[ \text{Original Array:} \] \[ A = [2,\; 5,\; 3,\; 1,\; 4,\; 2,\; 6] \]

\[ \text{Longest Non-Decreasing Subsequence:} \] \[ [\,\boxed{\color{#1a73e8}{2}},\; \color{black}{5},\; \boxed{\color{#1a73e8}{3}},\; \color{black}{1},\; \boxed{\color{#1a73e8}{4}},\; \color{black}{2},\; \boxed{\color{#1a73e8}{6}}\,] \]

Here, the boxed elements 2, 3, 4, 6 (shown in blue) form the longest non-decreasing subsequence. All the black elements are out of order and must be replaced.

\[ \text{Minimum Replacements} = n - \text{Length(LNDS)} = 7 - 4 = 3 \]

\[ \boxed{\text{Answer: Number of elements to be replaced} = 3} \]

Let’s visualize the replacements clearly:

\[ \begin{array}{ccccccc} \text{Original:} & 2 & 5 & 3 & 1 & 4 & 2 & 6 \\[6pt] \text{Replaced:} & 2 & \color{red}{2} & 3 & \color{red}{3} & 4 & \color{red}{4} & 6 \end{array} \]

Here, the values shown in red are the ones that need to be replaced to make the array sorted.


Follow-up Question ▼

Let the distance of an array \(A\), denoted as \(D(A)\), be defined as the minimum number of elements that must be replaced (with integers) so that the resulting array becomes sorted in non-decreasing order.

For \[ A = [2, 5, 3, 1, 4, 2, 6] \] if replacements are allowed only with integer values, determine how many distinct final sorted arrays can be obtained after making exactly \(D(A)\) replacements.

Hint ▼
\[ 2,\, \_,\, 3,\, \_,\, 4,\, \_,\, 6 \] \[ [\,2,\, \color{#d32f2f}{x_1},\, 3,\, \color{#1976d2}{x_2},\, 4,\, \color{#388e3c}{x_3},\, 6\,] \] \[ 2 \le \color{#d32f2f}{x_1} \le 3 \le \color{#1976d2}{x_2} \le 4 \le \color{#388e3c}{x_3} \le 6 \] \[ \color{#d32f2f}{x_1} \in \{2,3\}, \quad \color{#1976d2}{x_2} \in \{3,4\}, \quad \color{#388e3c}{x_3} \in \{4,5,6\} \]
• edited by
16 16 votes
Answer is 3.

Non decreasing means the resultant array should be increasing but not necessary strictly increasing.

Assume 0th order indexing.

Swap 5 and 2( at 5th index)

Swap 2(at 0th index ) and 1

Swap 3 and 2 ( at 3rd index)

So total 3 swaps are needed hence the minimum distance is 3.
7 7 votes

Here, 

arr = {2, 5, 3, 1, 4,  2, 6}

As per given in the Question " 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. "

So accordingly from the arr. {5, 1, 2} can be replaced with another integer such that '5 with 2 or 3', '1 with 3 or 4', '2 with 4 or 5 or 6'.

Distance = minimum number of elements in that must be replaced with another integer

Minimum distance = 3

Answer:
Position:
Show:

Related questions

25 25 votes
4 4 answers
22.2k
22.2k views
Arjun asked Feb 16, 2024
22,235 views
The number of distinct minimum-weight spanning trees of the following graph is
57 57 votes
16 16 answers
27.4k
27.4k views
Arjun asked Feb 16, 2024
27,414 views
​​​​​Let $\text{T(n)}$ be the recurrence relation defined as follows:\[\begin{array}{l}T(0)=1, \\T(1)=2, \text { and } \\T(n)=5 T(n-1)-6 T(n-2) \text { for } n \geq 2\end...
51 51 votes
6 6 answers
18.7k
18.7k views
Arjun asked Feb 16, 2024
18,707 views
​​​​Consider an array $\mathrm{X}$ that contains $\mathrm{n}$ positive integers. A subarray of $\mathrm{X}$ is defined to be a sequence of array locations with consecutiv...
44 44 votes
8 8 answers
25.7k
25.7k views
Arjun asked Feb 16, 2024
25,730 views
Let $\text{P}$ be the partial order defined on the set $\{1,2,3,4\}$ as follows\[P=\{(x, x) \mid x \in\{1,2,3,4\}\} \cup\{(1,2),(3,2),(3,4)\}\]The number of total orders ...