Minimum move to front operations
Basic Accuracy: 45.1% Submissions: 246 Points: 1

Given an array arr[] of size n such that array elements are distinct and in the range from 1 to n. The task is to count minimum number of move-to-front operations to arrange items as {1, 2, 3,… n}. The "move-to-front" operation is to pick any element arr[i] and place it at first position.

Example 1:

arr[] = {3, 2, 1, 4} 
Output: 2
We need to perform "move-to-front" on 1 
and 2 to make the array sorted.             

Example 2:

arr[] = {5, 7, 4, 3, 2, 6, 1} 
Output: 6

Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function minMoves() that takes array a and n as parameters and return the minimum number of move to front operation to sort the array.


Expected Time Complexity: O(N).
Expected Auxiliary Space:


1 ≤ N ≤ 106


We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Minimum move to front operations

Output Window