Pancake Sort

Problem Sort an array using ONLY a reverse(i) primitive that reverses the prefix [0..i] — no swaps or other mutations (pancake sorting).

Input / Output

  • Input: int array. Output: the sequence of flips (or the sorted array).

Constraints

  • n up to 100 classically; at most 2n flips expected — flip count, not comparisons, is the currency.

Example

  • [3,2,4,1]: bring max 4 to front with reverse(2) → [4,2,3,1], flip into place with reverse(3) → [1,3,2,4]; continue on the size-3 prefix.
asked …
LeaderboardSalaryAccount