Given an array of N integers, how can you find M elements to remove so that the array will end up in sorted order?
-
-
Answer:
First compute the longest nondecreasing subsequence in N log N time. A really simple algorithm to do this (that only uses arrays and binary search) can be found here: http://en.wikipedia.org/wiki/Longest_increasing_subsequence#Efficient_algorithms Let's suppose the longest subsequence has L elements. Then if L < N - M, there isn't any way to solve the problem since there's no subsequence of length N - M that's still sorted. Otherwise, just remove the N - L elements that aren't in the subsequence, and then remove more at random until exactly M total have been removed. In all this is an N log N algorithm.
Neal Wu at Quora Visit the source
Other answers
Assumption : Final sorted order is increasing order Step 1 : Find the longest increasing sequence in the given set of elements. This can be done in O(n^2) time. Suppose the number of elements are not present in the longest increasing sequence are 'k' . Now k <= m otherwise we could not get a sorted order by removing m elements because at least those elements that are not in the sequence needs to be removed . Step 2 : Remove any of the (m-k) elements from the longest increasing sequence to get a total of m elements removed. Now what we have is a sorted order of (n-m) elements. The complexity of algorithm is O(n^2) as step 1 takes O(n^2) time and step 2 takes O(m-k) time. If the final sorted order is needed as decreasing order, find the longest decreasing sequence in Step 1 instead of increasing sequence.
Sandesh Agrawal
This answer describes solution in O(N log N) time complexity. You know that you have array with N elements and after removing M elements there are N-M elements left in sorted order. So what is left is non-decreasing subsequence of length K, K=N-M. We can find such subsequence in time O(NlogN). Let's use dynamic programming here. Let d[i] be the least number that can be in the end of non-decreasing subsequence of length I. Base of dynamics: d[0] = -infinity. Rest of d is +infinity. Let's iterate through all N elements and for.every a[i] we will try to update array d. Simple code: For x in a For j in 0..k-1 If x >= d[i] and x < d[i+1] d[i+1] = x break It works in O(N*N) and now we will optimize it to O(NlogN). You can notice that with such algorithm d[i-1] is always less then or equal to d[i]. Another key moment is that every a[i] updates only one d[j] at max. (it is easy to prove, once you update one length there is no way it can update any further). These gives us a pretty much good place for binary search optimization. I mean, practically for every x = a[i], you need to find right most position j in d, so x is equal or greater than d[j] and d[j+1] is less then x. Set binary search and now you have the least number standing last in subsequence of length k for every k in 1..K. How to recover the answer? There is a common practice in dynamic programming, whenever you update some value, remember, what index updated this value and then it will be easy to recover the answer sequence from the end. Same time complexity can be reached using different approach with data structure called segment tree.
Slava Kim
Although finding the [math]M[/math] elements takes [math]O(N \log N)[/math] time, thereâs a nice approximation algorithm to find at most [math]2M[/math] elements such that the rest are in sorted order, in [math]O(N)[/math] timeâsee .
Anonymous
Related Q & A:
- How can I find out how many points i have on my license?Best solution by Yahoo! Answers
- How can I find an online job that I can start for free?Best solution by Yahoo! Answers
- How can I find out how many GBs I use a month on Internet?Best solution by Yahoo! Answers
- How can I find out what tax code I'm on?Best solution by Yahoo! Answers
- How can you have an unlawful eviction remove from credit report?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.