


You are given an array of integers ‘ARR’. Sort the array by performing a series of pancake flips. In one pancake flip, we do the following steps:
Choose an integer ‘K’ where 1 <= ‘K’ <= ARR.LENGTH.
Reverse the sub-array ARR[0, , ,K-1] (0-indexed).
Your task is to sort the array and return all the series of positions from where you flipped the array.
Note:
1. The array given will have elements between 1 to ‘N’, where N is the size of the array, and all elements of the array will be unique.
2. Any valid answer that sorts the array within 10 * array’s length flips will be judged as correct.
3. If the array is already sorted return an empty list.
For example:
If ARR = [3,2,1] and we performed a pancake flip choosing K = 3, we reverse the sub-array [3,2,1], so ARR = [1,2,3] . Hence the array becomes sorted therefore return {3}.
Input format :
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains a single integer N, where ‘N’ is the number of elements of the array.
The second line of each test case contains ‘N’ space-separated integers, denoting the array elements.
Output format :
For each test case, return the sequence of flips made.
The output of each test case will be printed in a separate line.
Note:
You don’t need to print anything. You just need to implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 100
1 <= ARR[i] < N
Time Limit: 1 second
2
3
3 2 1
4
4 1 2 3
3
4 3
For the first test case:
We performed a pancake flip choosing K = 3, we reverse the sub-array [3,2,1], so ARR = [1,2,3] . Hence the array becomes sorted.
For the second test case:
We first chose index 4, which made the array after reversing [3, 2, 1, 4]. Then we chose index 3 and reversed it hence giving the array [1, 2, 3, 4], which is sorted.
2
4
3 4 1 2
5
5 3 4 1 2
2 4 2
5 3 4 3
Fix one element at a time.
The main idea to solve this problem is that if we want to place a number at an index, then we need a maximum of 2 moves that are:-
O(N ^ 2), where ‘N’ is the length of the given array.
We find the index of every element and rotate the array two times max. Therefore, net time complexity will be O(N ^ 2).
O(1).
Since we are not using any extra space to keep track of the elements.