Given an array of n integers. Find the minimum number of elements from the array to remove or delete so that when the remaining elements are placed in the same sequence order form a sorted sequence
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
total answers (1)
So, for the sequence to be sorted we need to check for both the longest increasing and decreasing subsequence.
Let,
Longest increasing subsequence be known as LIS and Longest decreasing subsequence is LDS
So minimum elements to be deleted= array length- maximum(LIS, LDS)
Intuitively, the minimum value of maximum(LIS, LDS) would be 1 as each element represents the primitive sequence which is either increasing or decreasing one.
So, the base value is 1.
Now,
So,
To compute LIS(i), LDS(i) the recursion function is,
As, the base value is 1, for every index i, Lis(i), Lds(i) is at least 1.
Now, Minimum elements to be deleted =
To go through detailed explanation on LIS go through previous article on LIS: Longest Increasing Subsequence
LDS is quite similar like LIS, follow the recursion for LDS to understand this too.
C++ Implementation:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer