It was showing TLE on my solution. If possible please tell better approach.
You are given a permutation A of 1 to N with only even lengthed slopes. A slope is defined as a consecutive decreasing subarray.
Note: A subarray containing a single element is also considered a slope of length 1. For example, the sequence [5,1 , 4, 6, 2, 3] has 2 slopes of length 2, i.e., [5, 1] and [6, 2], and 2 slopes of length 1, i.e., [4] and [3].
Consider the following algorithm to sort the permutation:
re-sort(sequence A):
while(A is not in non decreasing order):
partion A into minimum number of consecutive slopes
for each slope with length > 1:
reverse(slope)Task
Determine the total number of times reverse() is called when the sequence is passed to re-sort()