In a row is a class of N students. You need to photograph the students, but for aesthetic reasons you want them to be ordered in order of growth. To achieve this, you can tell an adjacent group of students to change their positions. Your goal is to find the minimum number of students in an adjacent group who, after rearranging their positions, will result in the entire series being ordered in height.
The current location of students is given by array A, in which the element A K records the height of the student in position K. After the permutation, the students must be sorted in the order in which the height does not decrease. Those. A P ≤ A P + 1 for any 0 ≤ P <N-1.
For example, if array A has the following form:
A [0] = 1 A [1] = 2 A [2] = 6 A [3] = 5 A [4] = 5 A [5] = 8 A [6] = 9 The smallest group of students that need to be rearranged is A [2..4], length 3. After regrouping this group, we obtain [1,2,5,5,6,8,9], which is in the correct order. Any other permutation involving a contiguous group of less than three students will not result in the correct sorting of the resulting string.
The solution must contain a function that receives array A, containing the difference in height between the student and the photographer, and returns the minimum number of students in an adjacent group, after sorting which the entire array will be sorted. If the student array is already ordered, the function should return 0.
For example, for array A described above, the function should return 3.
Предположим, что: 1. N - целое число в диапазоне [1. 100 000] 2. каждый элемент массива А является целым числом в диапазоне [1. 100 000 000]. I would like to see your solution, preferably on JS (I am studying it).