Yesterday I asked this question about riffle shuffles. It seems that yesterdays question was a bit too hard so this question is a related but much easier task.

Today you are asked to determine if a permutation is in fact a riffle shuffle. Our definition of riffle shuffle is adapted from our last question:

The first part of the shuffle is the divide. In the divide partition the deck of cards in two. The two subsections must be continuous, mutually exclusive and exhaustive. In the real world want to make your partition as close to even as possible, however in this challenge this is not a consideration, all partitions including ones that are degenerate (one partition is empty) are of equal consideration.

After they have been partitioned, the cards are spliced together in such a way that cards maintain their relative order within the partition they are a member of. For example, if card A is before card B in the deck and cards A and B are in the same partition, card A must be before card B in the final result, even if the number of cards between them has increased. If A and B are in different partitions, they can be in any order, regardless of their starting order, in the final result.

Each riffle shuffle can then be viewed as a permutation of the original deck of cards. For example the permutation

1,2,3 -> 1,3,2

is a riffle shuffle. If you split the deck like so

1, 2 | 3

we see that every card in 1,3,2 has the same relative order to every other card in it's partition. 2 is still after 1.

On the other hand the following permutation is not a riffle shuffle.

1,2,3 -> 3,2,1

We can see this because for all the two (non-trivial) partitions

1, 2 | 3
1 | 2, 3 

there is a pair of cards that do not maintain their relative orderings. In the first partition 1 and 2 change their ordering, while in the second partition 2 and 3 change their ordering.


Given a permutation via any reasonable method, determine if it represents a valid riffle shuffle. You should output two distinct constant values one for "Yes, this is a riffle shuffle" and one for "No, this is not a riffle shuffle".

This is so answers will be scored in bytes with less bytes being better.

Test Cases

1,3,2 -> True
3,2,1 -> False
3,1,2,4 -> True
2,3,4,1 -> True
4,3,2,1 -> False
1,2,3,4,5 -> True
1,2,5,4,3 -> False
5,1,4,2,3 -> False
3,1,4,2,5 -> True
2,3,6,1,4,5 -> False
  • 1
    \$\begingroup\$ Can the output be inconsistent, but truthy / falsy in our language? Like (Python, where, among the integers only 0 is falsy) 0 for falsy but any integer in [1, +∞) for truthy? \$\endgroup\$ – Mr. Xcoder Jan 2 '18 at 16:50
  • 1
    \$\begingroup\$ @Mr.Xcoder I don't like the truthy/falsy values because they are rather hard to define well. Answers should stick to the current rules. \$\endgroup\$ – Wheat Wizard Jan 2 '18 at 16:53
  • \$\begingroup\$ Suggested test case: [3,1,4,2,5]. \$\endgroup\$ – Ørjan Johansen Jan 2 '18 at 17:28
  • 9
    \$\begingroup\$ Sorry about this, but: [2,3,6,1,4,5]. \$\endgroup\$ – Ørjan Johansen Jan 2 '18 at 18:14
  • 1
    \$\begingroup\$ Can we take permutations of [0, ..., n-1] instead of [1, ..., n] as input? \$\endgroup\$ – Dennis Jan 2 '18 at 22:37

14 Answers 14


JavaScript (ES6), 47 bytes

Takes input as an array of integers. Returns a boolean.


Test cases

let f =


  [1,3,2      ],  // -> True
  [3,2,1      ],  // -> False
  [3,1,2,4    ],  // -> True
  [2,3,4,1    ],  // -> True
  [4,3,2,1    ],  // -> False
  [1,2,3,4,5  ],  // -> True
  [1,2,5,4,3  ],  // -> False
  [3,1,4,2,5  ],  // -> True
  [2,3,6,1,4,5]   // -> False
.forEach(a => console.log(JSON.stringify(a) + ' -> ' + f(a)))


The input array A is a valid riffle shuffle if it consists of at most two distinct interlaced sequences of consecutive integers.

The challenge rules specify that we're given a permutation of [1...N]. So we don't need to additionally check that the sorted union of these sequences actually results in such a range.

We use a counter x initialized to A[0] and a counter y initially undefined.

For each entry z in A, starting with the 2nd one:

  • We check whether z is equal to either x+1 or y+1. If yes, we increment the corresponding counter.
  • Else: if y is still undefined, we initialize it to z.
  • Else: we make the test fail.

Haskell, 41 bytes

f l=elem(foldl(%)0$l++l)l

Try it online!

Checks if the list concatenated to itself contains the numbers 0..n-1 in order as a subsequence.


Haskell, 43 bytes

s takes a list of integers as in the OP examples and returns a Bool.

s p=or[f(<x)p++f(>=x)p<[1..]|x<-p]

Try it online!

How it works

  • The list comprehension tries each element x of p in turn and checks if it can be the first element of the second partition of the shuffle. Then or returns True if any of the checks were True.
  • The comprehension does this by partitioning (with filter) p into elements smaller and larger than (or equal to) x, concatenating, and checking if the resulting list is [1..length p], i.e. the elements in order.
  • The check for whether the resulting list is [1..length p] is done by seeing if the result is strictly smaller than the infinite list [1..] == [1,2,3,etc.], which gives the same result for any permutation.

Jelly, 13 6 bytes


Try it online!

Alternate version, postdates challenge, 5 bytes


Try it online!

How it works

ỤIṢḊRẠ  Main link. Argument: A (permutation of [1, ..., n])

Ụ       Grade up; sort the indices of A by their respective values.
        For shuffles, the result is the concatenation of up to two increasing
        sequences of indices.
 I      Compute the forward differences.
        In a shuffle, only one difference may be negative.
  Ṣ     Sort the differences.
   Ḋ    Dequeue; remove the first (smallest) difference.
    R   Range; map each k to [1, ..., k].
        This yields an empty array for non-positive values of k.
     Ạ  All; check if all resulting ranges are non-empty.

Python 2, 39 bytes

for x in l*2:n+=n==x

Try it online!

Takes the list 0-indexed and outputs via exit code.


Brachylog, 9 bytes


Try it online!

The predicate succeeds if the input represents a riffle shuffle and fails if it does not, meaning among other things that if the predicate is run as an entire program success will print true. and failure will print false.. Input is taken as a list of any sorts of items and interprets it as representing a permutation of itself sorted.

   Ċ         Some length-two list
 ~c          which concatenated
o            is the input sorted
    ⟨        satisfies the condition that its first element
     ⊆       is an ordered not-necessarily-contiguous sublist
        ?    of the input
      ⊇      which is an ordered superlist of
       ⟩     the list's second element.

I feel like there's something in the spirit of ⊆ᵐ which should work in the place of the four-byte "sandwich" construct ⟨⊆⊇⟩.

  • 1
    \$\begingroup\$ I think you are the first person to use a sandwich in a PPCG answer (and it's a beautiful symmetric one :)) \$\endgroup\$ – Fatalize Mar 6 '19 at 15:20

Python 2, 75 60 47 bytes

thanks to Dennis for -13 bytes

for v in input():x=x-{v-1}|{v}

Try it online!

Output is via exit code.


Ruby, 35 bytes


Try it online!


  • l & [*1..a] | l applies an intersection and then an union: first get the elements of l that are <=a and then add the remaining elements of l without changing the order. If a is the number we are looking for, then this operation is the same as sorting l.

Clean, 56 55 bytes

import StdEnv
?l=any(\e=sortBy(\a b=a<e&&b>=e)l<[1..])l

Try it online!


Pyth, 5 bytes


Test suite


    +QQ  concatenated two copies of the (implicit) input
   y     all subsequences of it
}        contain an element equaling
 SQ      the input list sorted 

Checks whether the doubled input list contains a sorted version of itself as a subsequence.

Thanks to Erik the Outgolfer for 1 byte taking better advantage of implicit input with +QQ rather than *2Q.

  • \$\begingroup\$ 5 bytes: }SQy+. It gets expanded to }SQy+QQ. \$\endgroup\$ – Erik the Outgolfer Jan 7 '18 at 18:43
  • \$\begingroup\$ @EriktheOutgolfer Nice one, thanks. \$\endgroup\$ – xnor Jan 10 '18 at 20:34

Pyth, 9 bytes


Test suite.

Saved 3 bytes thanks to isaacg.

Pyth, 14 bytes


Try it here! or Verify all the test cases.

Outputs True and False for riffle-shuffle and non-riffle-shuffle respectively.


}SQm.nS.Tcd2./ ~ Full program. Reads input from STDIN and outputs to STDOUT.

            ./ ~ Return all divisions of the input into disjoint substrings (partition).
   m           ~ Map over the above using a variable d.
         cd2   ~ Chop d into two-element lists.
       .T      ~ Justified transpose, ignoring absences.
      S        ~ Sort (lexicographically).
    .n         ~ Deep-flatten.
}              ~ Check if the above contains...
 SQ            ~ The sorted input.
  • \$\begingroup\$ Furthermore, <#0 can be replaced by - for 2 more bytes. \$\endgroup\$ – isaacg Jan 2 '18 at 22:41
  • \$\begingroup\$ @isaacg Oh yeah facepalm thanks. Edited. Edited. \$\endgroup\$ – Mr. Xcoder Jan 2 '18 at 22:41

Husk, 7 bytes


Test suite!


Perl, 28 bytes

Includes +1 for a

Input on STDIN, 1 based

perl -aE '$.+=$_==$.for@F,@F;say$.>@F' <<< "3 1 2 4"

Uses xnor's algorithm


C (gcc), 61 bytes


Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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