Given an array of size n such that array elements are in range from 1 to n. The task is to count number of move-to-front operations to arrange items as {1, 2, 3,… n}. The **move-to-front** operation is to pick any item and place it at first position.

This problem can also be seen as a stack of items with only move available is to pull an item from the stack and placing it on top of the stack.

Examples :

Input:arr[] = {3, 2, 1, 4}.Output:2 First, we pull out 2 and places it on top, so the array becomes (2, 3, 1, 4). After that, pull out 1 and becomes (1, 2, 3, 4).Input:arr[] = {5, 7, 4, 3, 2, 6, 1}Output:6 We pull elements in following order 7, 6, 5, 4, 3 and 2Input:arr[] = {4, 3, 2, 1}.Output:3

The idea is to traverse array from end. We expect n at the end, so we initialize expectedItem as n. All the items which are between actual position of expectedItem and current position must be moved to front. So we calculate the number of items between current item and expected item. Once we find expectedItem, we look for next expectedItem by reducing expectedITem by one.

Following is the algorithm for the minimum number of moves:

1. Initialize expected number at current position as n 2. Start from the last element of array. a) If the current item is same as expected item, decrease expected item by 1. 3. Return expected item.

Below is the implementation of this approach.

## C++

`// C++ program to find minimum number of move-to-front ` `// moves to arrange items in sorted order. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Calculate minimum number of moves to arrange array ` `// in increasing order. ` `int` `minMoves(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Since we traverse array from end, extected item ` ` ` `// is initially n ` ` ` `int` `expectedItem = n; ` ` ` ` ` `// Taverse array from end ` ` ` `for` `(` `int` `i=n-1; i >= 0; i--) ` ` ` `{ ` ` ` `// If current item is at its correct position, ` ` ` `// decrement the expectedItem (which also means ` ` ` `// decrement in minimum number of moves) ` ` ` `if` `(arr[i] == expectedItem) ` ` ` `expectedItem--; ` ` ` `} ` ` ` ` ` `return` `expectedItem; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `int` `arr[] = {4, 3, 2, 1}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `cout << minMoves(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program to find minimum ` `// number of move-to-front moves ` `// to arrange items in sorted order. ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Calculate minimum number of moves ` ` ` `// to arrange array in increasing order. ` ` ` `static` `int` `minMoves(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `// Since we traverse array from end, ` ` ` `// extected item is initially n ` ` ` `int` `expectedItem = n; ` ` ` ` ` `// Taverse array from end ` ` ` `for` `(` `int` `i = n - ` `1` `; i >= ` `0` `; i--) ` ` ` `{ ` ` ` `// If current item is at its correct position, ` ` ` `// decrement the expectedItem (which also means ` ` ` `// decrement in minimum number of moves) ` ` ` `if` `(arr[i] == expectedItem) ` ` ` `expectedItem--; ` ` ` `} ` ` ` ` ` `return` `expectedItem; ` ` ` `} ` ` ` ` ` `// Driver Program ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `arr[] = {` `4` `, ` `3` `, ` `2` `, ` `1` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println( minMoves(arr, n)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find minimum ` `# number of move-to-front moves ` `# to arrange items in sorted order. ` ` ` `# Calculate minimum number of moves ` `# to arrange array in increasing order. ` `def` `minMoves(arr, n): ` ` ` ` ` `# Since we traverse array from end, ` ` ` `# expected item is initially n ` ` ` `expectedItem ` `=` `n ` ` ` ` ` `# Taverse array from end ` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` ` ` `# If current item is at its ` ` ` `# correct position, decrement ` ` ` `# the expectedItem (which also ` ` ` `# means decrement in minimum ` ` ` `# number of moves) ` ` ` `if` `(arr[i] ` `=` `=` `expectedItem): ` ` ` `expectedItem ` `-` `=` `1` ` ` `return` `expectedItem ` ` ` `# Driver Code ` `arr ` `=` `[` `4` `, ` `3` `, ` `2` `, ` `1` `] ` `n ` `=` `len` `(arr) ` `print` `(minMoves(arr, n)) ` ` ` `# This code is contributed 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// number of move-to-front moves ` `// to arrange items in sorted order. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Calculate minimum number of moves ` ` ` `// to arrange array in increasing order. ` ` ` `static` `int` `minMoves(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` `// Since we traverse array from end, ` ` ` `// extected item is initially n ` ` ` `int` `expectedItem = n; ` ` ` ` ` `// Taverse array from end ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) ` ` ` `{ ` ` ` `// If current item is at its ` ` ` `// correct position, decrement ` ` ` `// the expectedItem (which also ` ` ` `// means decrement in minimum ` ` ` `// number of moves) ` ` ` `if` `(arr[i] == expectedItem) ` ` ` `expectedItem--; ` ` ` `} ` ` ` ` ` `return` `expectedItem; ` ` ` `} ` ` ` ` ` `// Driver Program ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `[]arr = {4, 3, 2, 1}; ` ` ` `int` `n = arr.Length; ` ` ` `Console.Write( minMoves(arr, n)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum ` `// number of move-to-front moves ` `// to arrange items in sorted order. ` ` ` `// Calculate minimum number of ` `// moves to arrange array ` `// in increasing order. ` `function` `minMoves(` `$arr` `, ` `$n` `) ` `{ ` ` ` `// Since we traverse array ` ` ` `// from end, extected item ` ` ` `// is initially n ` ` ` `$expectedItem` `= ` `$n` `; ` ` ` ` ` `// Taverse array from end ` ` ` `for` `(` `$i` `= ` `$n` `- 1; ` `$i` `>= 0; ` `$i` `--) ` ` ` `{ ` ` ` `// If current item is at its ` ` ` `// correct position, decrement ` ` ` `// the expectedItem (which also ` ` ` `// means decrement in minimum ` ` ` `// number of moves) ` ` ` `if` `(` `$arr` `[` `$i` `] == ` `$expectedItem` `) ` ` ` `$expectedItem` `--; ` ` ` `} ` ` ` ` ` `return` `$expectedItem` `; ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `(4, 3, 2, 1); ` `$n` `= ` `count` `(` `$arr` `); ` `echo` `minMoves(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

3

**Time Complexity : **O(n)

This article is contributed by **Anuj Chauhan(anuj0503)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the minimum number of moves to reach end of the array
- Minimum number of moves to make a binary array K periodic
- Minimum number of swaps required to sort an array of first N number
- Minimum number of swaps required to sort an array
- Minimum number of swaps required to sort an array | Set 2
- Minimum number of moves to make all elements equal
- Sort an array according to count of set bits | Set 2
- Sort an array according to count of set bits
- Minimum range increment operations to Sort an array
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Minimum increment or decrement required to sort the array | Top-down Approach
- Minimum adjacent swaps required to Sort Binary array
- Sort an array of strings based on count of distinct characters
- Sort an array according to the increasing count of distinct Prime Factors
- Count of smaller elements on right side of each element in an Array using Merge sort
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Maximum and minimum of an array using minimum number of comparisons
- Sort an Array alphabetically when each number is converted into words
- Find array with k number of merge sort calls
- Count minimum steps to get the given desired array