Count minimum number of "move-to-front" moves to sort an array
Last Updated :
19 Sep, 2023
Given an array of size n such that array elements are in range from 1 to n. The task is to count a 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 in 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 2
Input: 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, expected item
// is initially n
int expectedItem = n;
// Traverse 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;
}
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,
// expected item is initially n
int expectedItem = n;
// Traverse 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
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
# Traverse 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
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,
// expected item is initially n
int expectedItem = n;
// Traverse 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.
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, expected item
// is initially n
$expectedItem = $n;
// Traverse 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.
?>
JavaScript
<script>
// JavaScript 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,
// expected item is initially n
let expectedItem = n;
// Traverse array from end
for (let 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;
}
let arr = [4, 3, 2, 1];
let n = arr.length;
document.write( minMoves(arr, n));
</script>
Time Complexity : O(n)
Auxiliary Space: O(1)
Similar Reads
Minimum cost to sort a matrix of numbers from 0 to n^2 - 1 Given an n x n matrix containing all the numbers in the range 0 to n2-1. The problem is to calculate the total energy required for rearranging all the numbers in the matrix in strictly increasing order, i.e., after the rearrangement, the 1st row contains 'n' numbers from 0 to n-1, then 2nd row from
8 min read
Minimum number of swaps required to sort an array of first N number Given an array arr[] of distinct integers from 1 to N. The task is to find the minimum number of swaps required to sort the array. Example: Input: arr[] = { 7, 1, 3, 2, 4, 5, 6 } Output: 5 Explanation: i arr swap (indices) 0 [7, 1, 3, 2, 4, 5, 6] swap (0, 3) 1 [2, 1, 3, 7, 4, 5, 6] swap (0, 1) 2 [1,
5 min read
Minimum range increment operations to Sort an array Given an array containing N elements. It is allowed to do the below move any number of times on the array: Choose any L and R and increment all numbers in range L to R by 1. The task is to find the minimum number of such moves required to sort the array in non decreasing order. Examples: Input : arr
5 min read
Count number of 1s in the array after N moves Given an array of size N in which initially all the elements are 0(zero). The task is to count the number of 1's in the array after performing N moves on the array as explained:In each move (starting from 1 to N) the element at the position of the multiple of the move number is changed from 0 to 1 o
9 min read
Minimum number of moves required to sort Array by swapping with X Given an integer array, arr[] of size N and an integer X. The task is to sort the array in increasing order in a minimum number of moves by swapping any array element greater than X with X any number of times. If it is not possible print -1. Examples: Input: arr[] = {1, 3, 4, 6, 5}, X = 2Output: 3Ex
7 min read
Minimum operations to sort Array by moving all occurrences of an element to start or end Given an array arr[] of size N where arr[i] ⤠N, the task is to find the minimum number of operations to sort the array in increasing order where In one operation you can select an integer X and: Move all the occurrences of X to the start orMove all the occurrences of X to the end. Examples: Input:
15 min read
Minimum number of deques required to make the array sorted Given an array arr containing N unique integers. The task is to calculate the minimum number of deques required to make the array sorted. Example: Input: arr[] = {3, 6, 0, 9, 5, 4}Output: 2Explanation: Create a new deque d1 = {3}.Create a new deque d2 = {6}.Push 0 onto the front of d1; d1 = {0, 3}Pu
9 min read
Minimum number of swaps required to sort an array | Set 2 Given an array of N distinct elements, find the minimum number of swaps required to sort the array. Note: The problem is not asking to sort the array by the minimum number of swaps. The problem is to find the minimum swaps in which the array can be sorted. Examples: Input: arr[] = {4, 3, 2, 1} Outpu
7 min read
Total number of components in the index Array Given an array arr[] of N integers of value from 0 to N, the task is to count the number of components in Index Array. Index array means if we are at ith index then it leads to arr[i]. The component of an index array is counted when it forms a cycle. If no cycle persists or the array contains a sing
12 min read