1
// BruteForce Approach!
2
// Author - Nikhil Sharma
3
// LinkedIn - https://www.linkedin.com/in/nikhil-sharma-41a287226/
4
// Twitter - https://twitter.com/Sharma_Nikh12
5

6
class Solution {
7
public List<Integer> pancakeSort(int[] arr) {
8
List<Integer> list = new ArrayList<>();
9
int n = arr.length;
10
while (n != 1) {
11
int maxIndex = findIndex(arr, n);
12
reverse(arr, maxIndex);
13
reverse(arr, n - 1);
14
list.add(maxIndex + 1);
15
list.add(n);
16
n--;
17
}
18
return list;
19
}
20

21
static int findIndex(int[] arr, int value) {
22
for (int i = 0; i < arr.length; i++) {
23
if (arr[i] == value) {
24
return i;
25
}
26
}
27
return 0;
28
}
29

30
static void reverse(int[] arr, int maxIndex) {
31
int l = 0;
32
while (l < maxIndex) {
33
int temp = arr[l];
34
arr[l] = arr[maxIndex];
35
arr[maxIndex] = temp;
36
l++;
37
maxIndex--;
38
}
39
}
40
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0