Please answer in C++, show compiled output as well thank you given N numbers in p1,p2,p3…pn, we would like to sort these number. The only process we can do is moving the first number in the list k spaces down the line. For example, suppose that N=4 and the numbers start off in the following order: 4, 3, 2, 1 The only number can be moved at this point is 4, If we instructs number 4 to move 2 spaces down the line, the order will subsequently look like this: 3, 2, 4, 1 Now the only number can be moved is 3, so in the second time step we may give number 3 an instruction, and so forth until the list of numbers are sorted. Find the minimum number of time steps required to sort these numbers. INPUT FORMAT: The first line of input contains N, The second line contains N space-separated integers, p1,p2,p3…pn, indicating the starting order of these numbers. OUTPUT FORMAT A single integer: the number of time steps before the N numbers are in sorted order, if we acts optimally. SAMPLE INPUT 1: 4 1 2 4 3 SAMPLE OUTPUT 1: 3 SAMPLE INPUT 2: 60 35 50 4 39 54 32 20 53 29 18 55 28 40 44 48 25 21 5 9 14 33 3 16 38 31 23 15 60 27 49 11 51 46 52 30 45 1 6 36 43 24 7 58 56 42 2 8 10 12 13 17 19 22 26 34 37 41 47 57 59 SAMPLE OUTPUT 2: 45
Please answer in C++, show compiled output as well thank you
given N numbers in p1,p2,p3…pn, we would like to sort these
number. The only process we can do is moving the first number in
the list k spaces down the line.
For example, suppose that N=4 and the numbers start off in the
following order:
4, 3, 2, 1
The only number can be moved at this point is 4, If we instructs
number 4 to move 2 spaces down the line, the order will
subsequently look like this:
3, 2, 4, 1
Now the only number can be moved is 3, so in the second time
step we may give number 3 an instruction, and so forth until the
list of numbers are sorted.
Find the minimum number of time steps required to sort these
numbers.
INPUT FORMAT:
The first line of input contains N, The second line
contains N space-separated integers, p1,p2,p3…pn, indicating the
starting order of these numbers.
OUTPUT FORMAT
A single integer: the number of time steps before the N numbers
are in sorted order, if we acts optimally.
SAMPLE INPUT 1:
4
1 2 4 3
SAMPLE OUTPUT 1:
3
SAMPLE INPUT 2:
60 35 50 4 39 54 32 20 53 29 18 55 28 40 44 48 25 21 5 9 14 33 3 16
38 31 23 15 60 27 49 11 51 46 52 30 45 1 6 36 43 24 7 58 56 42 2
8 10 12 13 17 19 22 26 34 37 41 47 57 59
SAMPLE OUTPUT 2:
45
Step by step
Solved in 3 steps