Skip to content

Practice Pancake Sorting

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#969
LeetCode Problem

Pancake Sorting

Problem Statement

Sort arr using prefix reversals. In one flip, choose a length k from 1 through arr.length and reverse the first k elements. Return a sequence of those chosen lengths that sorts the array in nondecreasing order. Any valid sequence with at most 10 * arr.length flips is accepted.

Source: Pancake Sorting on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
arr = [3,2,4,1]
Output
[4,2,4,3]

# Example 2

Input
arr = [1,2,3]
Output
[]

Constraints

1 <= arr.length <= 100
1 <= arr[i] <= arr.length
All integers in arr are unique (i.e. arr is a permutation of the integers from 1 to arr.length).

Related LeetCode Problems

Practice more problems similar to Pancake Sorting.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.