
You are given,
array ARR=[4,3,2,1]
So, the output is 16 as we can take the sub-array [4,3,2,1](from index 0 to index 4) and sort it. So the total cost is 4*4 (16).
The first line contains a single integer ‘T’ representing the number of test cases. Then each test case follows.
The first line of each test case contains one integers ‘N’ denoting the number of elements in the array.
The next line of each test case contains ‘N’ integers of the array( ARR).
For each test case, return the minimum cost required to sort the whole array.
The output of each test case should be printed in a separate line.
You are not required to print anything, it has already been taken care of. Just implement the function.
1 <= T <= 5
3 <= N <= 10^5
1 <= ARR[i] <= 10^9
Time Limit: 1 sec
First, we keep a sorted version of the array arr. Now we start iterating in the original array. If the count of the elements in the original array is equal to the count of the elements in the sorted array we sort this sub-array and go on.
First, we keep a sorted version of the array arr. Now we start iterating in the original array. If the count of the elements in the original array is equal to the count of the elements in the sorted array we sort this sub-array and go on. We use an ordered map to search more efficiently.