Crossing Sequences
Given a list of positive integers A
, call it an increasing sequence if each element is greater than or equal to the previous one; and call it a decreasing sequence if each element is less than or equal to the previous one.
Some increasing sequences:
[1,2,4,7]
[3,4,4,5]
[2,2,2]
[]
Some decreasing sequences:
[7,4,2,1]
[5,4,4,3]
[2,2,2]
[]
A crossing sequence is a list that can be decomposed into two disjoint subsequences, one an increasing sequence and the other a decreasing sequence.
For example, the list:
[3,5,2,4,1]
is a crossing sequence, since it can be decomposed into:
[3, 4 ]
[ 5,2, 1]
where [3,4]
is the increasing subsequence and [5,2,1]
is the decreasing subsequence. We'll call such a pair of (increasing,decreasing) subsequences a decomposition of the crossing sequence.
The list:
[4,5,2,1,3]
is not a crossing sequence; there is no way to decompose it into an increasing and decreasing subsequence.
Your task is to write a program/function taking as input a list of positive integers; and if it is a crossing sequence, return the two lists in one of its decompositions; or some consistent "falsey" value if the list is not a crossing sequence.
This is code-golf; shortest program/function in each language is the winner.
Rules:
- Input is flexible.
- The usual loopholes are forbidden.
- If there are multiple valid ways to decompose the input, you may output one or all of them.
- Output formatting for the decomposition is flexible; but it must be unambiguous regarding the distinction between the two subsequences.
- You may use any consistent output value to indicate that the input is not a crossing sequence; so long as it is unambiguous compared to the output for any crossing sequence. You should specify the falsey value in your answer.
Test Cases:
Using False
to indicate non-crossing sequences:
[3, 5, 2, 4, 1] => [3, 4], [5, 2, 1]
[3, 5, 2, 4, 4, 1, 1] => [3, 4, 4], [5, 2, 1, 1]
[7, 9, 8, 8, 6, 11] => [7, 8, 8, 11], [9, 6]
[7, 9, 8, 8, 6, 11] => [7, 9, 11], [8, 8, 6] # also valid
[7, 9, 8, 8, 6, 11] => [7, 8, 11], [9, 8, 6] # also valid
[7, 8, 9, 10, 20, 30] => [7, 8, 9, 20, 30], [10]
[7, 8, 9, 10, 20, 30] => [8, 9, 10, 20, 30], [7] # this is also valid
[5, 5, 5] => [5, 5, 5], []
[4, 5, 2, 1, 3] => False
[3, 4, 3, 4, 5, 2, 4] => False
[3, 5, 2, 4, 4, 1, 1]
. The current test cases let you get away with>=
/<
, when it should really be>=
/<=
. \$\endgroup\$