You're given an array of positive integers. Your job is to sort them using an initially empty deque (double-ended queue) by the following procedure:
- Take a number from the front of the array, and push it into one of the two ends of the deque. Repeat this until the array is empty.
Determine if it is possible that the deque is sorted from left to right in the end.
You may assume that the array is not empty. The input numbers are not necessarily distinct.
For output, you can choose to
- output truthy/falsy using your language's convention (swapping is allowed), or
- use two distinct, fixed values to represent true (affirmative) or false (negative) respectively.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
Truthy:
[1]
[1, 2, 3, 4]
[4, 3, 2, 1]
[10, 10, 10, 10]
[3, 4, 2, 5, 1, 6]
[4, 3, 5, 2, 6, 1]
[5, 4, 4, 5, 3, 5, 3, 6, 2, 6]
Falsy:
[1, 3, 2]
[3, 1, 2]
[4, 3, 2, 3, 2, 1]
[1, 2, 3, 2, 3, 4]
[4, 5, 4, 3, 4, 5]