0

I want to find the maximal subset of an array which is sorted in ascending order

Say I have

a = [2, 1, 4, 6, 7]
a_max_subset = [1, 4, 6, 7]

b = [4, 1, 2]
b_max_subset = [1, 2] 

c = [2, 5, 13, 8, 6, 23, 33]
c_max_subset = [2, 5, 8, 23, 33]

Is there an efficient way to do this ?

7
  • 5
    Largest increasing subsequence perhaps? Commented Jul 9, 2015 at 4:08
  • 4
    I don't think there can be anything better than O(n) anyway, as you really have to though the array from beginning to end. Commented Jul 9, 2015 at 4:11
  • Do the elements have to appear consecutively in the original array? e.g. c = [3, 6, 4, 5, 7], is c_max_subset [3, 4, 5, 7] or [4, 5, 7]? Commented Jul 9, 2015 at 4:23
  • @samgak c_max_subset will be [3, 4, 5, 7] Commented Jul 9, 2015 at 6:02
  • Why can't b_max_subset be [4, 1]? Commented Jul 9, 2015 at 6:15

1 Answer 1

0

What you are trying to find out is Longest increasing subsequence.

You can find an O(N^2) implementation here. It's well explained and commented properly. Try to understand the explanation and implement it by yourself.

If you are really curious, you can check this O(N logN) solution also.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.