There is a "sorting algorithm" sometimes called Stalin sort in which in order to sort a list you simply remove elements from the list until it is sorted in increasing order. For example the list

[1, 2, 4, 5, 3, 6, 6]

When "sorted" using Stalin sort becomes

[1, 2, 4, 5, 6, 6]

The three was removed because it was out of order.

Now obviously there are many ways to remove elements to sort a list. For example any list with less than two elements must be sorted so by just removing enough elements blindly we can always sort a list. Since this is the case we only care about the longest possible result from a Stalin sort.

Your task will be to take a list of positive integers and output the length of the longest sorted (increasing) list that can be arrived at by removing elements from the original list. That is find the length of the longest sorted (possibly non-contiguous) sub-list.

Sorted lists can have the same element more than once in a row. You do not need to support the empty list unless your program itself is empty.


Your answer will be scored by the length of its own longest possible Stalin sort. Programs will be interpreted as a sequence of bytes rather than characters, and their order will be the natural one that arises by interpreting the bytes as numbers. Lower scores are better.

This is not

Here's a neat-o tool to help you score your answers.

Test cases

[1, 2, 4, 5, 3, 6, 6] -> 6
[19, 2] -> 1
[3, 3, 4, 3] -> 3
[10] -> 1
[1, 2, 4, 9] -> 4
[1, 90, 2, 3, 4, 5] -> 5
[1, 90, 91, 2, 3, 4, 5] -> 5
  • 3
    \$\begingroup\$ In short: output the length of the longest (non-strictly) increasing sequence. \$\endgroup\$
    Oct 30, 2018 at 15:16
  • 1
    \$\begingroup\$ I like the rule "You do not need to support the empty list unless your program itself is empty." \$\endgroup\$ Nov 1, 2018 at 0:24
  • \$\begingroup\$ This challenge reminds me a lot of the dropsort challenge: codegolf.stackexchange.com/questions/61808/… \$\endgroup\$
    – Stefnotch
    Nov 1, 2018 at 20:24
  • 1
    \$\begingroup\$ I made a checker at ptpb.pw/SVSt.html . Still not very functional, but it works. (TODO: * bar chart * partition into least decreasing sequences * support for other code pages) \$\endgroup\$
    Nov 2, 2018 at 16:17
  • \$\begingroup\$ @user202729 Cool! I've added it to the post. Feel free to edit newer versions in if necessary. \$\endgroup\$
    – Wheat Wizard
    Nov 2, 2018 at 21:37

9 Answers 9


Python 2, length 14 12 10 9

for	a	in	i:L[a-1]=M(L[:a])+1

Output is via exit code.

Try it online!

How it works

At all times, the array \$L\$ keeps track of the longest sorted subarrays encountered so far; \$L[a-1]\$ is the length of longest one that ends with \$a\$.

Initially, we haven't processed an array elements, so \$L\$ consists entirely of zeroes.

When processing the array element \$a\$, we first take the maximum of \$[L[0], \dots, L[a-1]]\$, which is length of the longest sorted subarray encountered so far that ends with \$a\$ or a smaller integer. Appending \$a\$ to such an array will keep it sorted, so the longest sorted subarray ending in \$a\$ is one element longer than that maximum. We update \$L[a-1]\$ with the computed value.

The final result is the maximum of \$L\$.

  • \$\begingroup\$ Can you please explain, why it works? I'm having hard times to comprehend it :( \$\endgroup\$ Oct 31, 2018 at 14:01
  • \$\begingroup\$ I've added an explanation. \$\endgroup\$
    – Dennis
    Oct 31, 2018 at 15:54

Wolfram Language (Mathematica), score 9


Try it online!


Jelly, length  4  2


Try it online!

Bytes in Jelly's code page

183 146 144 90 76 169 125 19 80

How it works

ṢƑƇZLƲ}ŒP  Main link. Argument: A (array)

       ŒP  Powerset; yield P, the array of all sub-arrays of A.
     Ʋ     Vier; combine the preceding four links into a monadic chain...
      }    and apply the chain to the right argument (P).
  Ƈ            Comb; only keep arrays for which the link to the left returns 1.
ṢƑ             Sort fixed; yield 1 if sorting doesn't alter the array.
   Z           Zip; read the filtered powerset by columns.
    L          Take the length.

Perl 6, score 9

*.combinations.map({@^a*[<=] @a}).max

Try it online!


Haskell, Score 8 7, 48 bytes


Try it online!

The longest sorted sublist is


Pyth, score 3 2 (7 bytes)


Saved a point thanks to Anders Kaseorg.
Try it here


     yQ    Take the power set of the (implicit) input (preserving order).
  SI#      Get the ones that are sorted.
 e         Take the last (longest).
l          Get the length.
  • \$\begingroup\$ leSI#y scores 2. \$\endgroup\$ Oct 30, 2018 at 21:53

Stax, 4 maximal length stalin sort


Run and debug it

It works like this.

S       powerset of input
{:^f    filter by non-descending sequences
F%|M    take the maximum length remaining

Brachylog, length 2 (4 bytes)


Try it online!

An answer which makes up for being so concise by not being that much shorter sorted.

(08 03 80 6C in Brachylog's code page)

   l    the length of
 ≤₁     a non-decreasing
⊇       sublist of
        the input.
        (maximizing the size of the sublist)
  • \$\begingroup\$ I came up with ►LSnmOṖ for Husk but its score (for its length at least) is too bad to bother posting... \$\endgroup\$ Mar 25, 2019 at 7:52

R, Score 15 11, 72 62 bytes

function(L,M=max,A=1:M(L)*0){for(Y in L)A[Y]=M(A[1:Y])+1;M(A)}

Try it online!

Ports Dennis' Python answer to R.

  • \$\begingroup\$ Just changing variable names won't help, because as your last link shows, none of them are used in the (found) substring that gives score 15. \$\endgroup\$ Oct 31, 2018 at 1:10
  • \$\begingroup\$ @ØrjanJohansen ah, of course, I'm rather dumb. I suppose another approach is necessary. \$\endgroup\$
    – Giuseppe
    Oct 31, 2018 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.