Solve this problem in the fewest number of bytes of code possible.
We have a data variable that contains user input data. Data is a list of integers. Write the code that finds the longest bitonic subarray in the data list.
A bitonic subarray is a sub-list that first increases and then decreases (or always increases or always decreases).
Example:
[1,2,5,5,9,9,3,2]
[1,2,3]
[-5,3,2,0,-4]
Constraints and additional rules: If there are several longest bitonic subarrays in the array, then return the first one.
Test cases:
[1] => [1]
[-1,2,1,3,1,4,1,5,1] => [-1,2,1]
[3,2,3,4,5,3,2,1,5,2,3] => [2,3,4,5,3,2,1]
[1,2,3,2,3,4,5,3,2,1,5,2,3] => [2,3,4,5,3,2,1]
[1,2,5,6,3,2,1,0,6,3,2,1,0] => [1,2,5,6,3,2,1,0]
[3,3,2,2,3,3,4,4,4,5,5,5,5,3,3,2,2,1,1,5,5,2,3] => [2,2,3,3,4,4,4,5,5,5,5,3,3,2,2,1,1]
[1,2,3,4,5] => [1,2,3,4,5]
[5,4,3,2,1] => [5,4,3,2,1]
P.S.: I really would like to see a one-line C# answer
1,3,1
is also valid for second testcase? \$\endgroup\$[1,2,2,1,5,5,5,5,5,1,3,3,3,1,4,4,4,4,1] => [5,5,5,5,5,1,3,3,3]
. Currently, the only test case with equal runs lets you greedily assume that they're "going in the direction of" the last change between runs. One solution I was sketching in my head fails this, and it seems that tsh's Python solution does as well (after increasing the recursion limit in the header). \$\endgroup\$[5,1,3]
is decreasing and then increasing, which isn't a valid bitonic sub-array. If the1
in the middle would be a7
the output would be[1,5,5,5,5,5,7,3,3,3,1]
instead. For which my answer seems to fail unfortunately, since I output[1,5,5,5,5,5,7,3]
instead.. :( Will fix it. \$\endgroup\$