- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given with an array of numbers between 1 to n. The goal here is to find the no. of ‘move to front’ operations required to sort the given array. The array has no repetition. The ‘move to front’ operation picks an element and places at first position, here at index 0.

We will traverse the array from end, if element is at the correct position then no move else move is required. For elements from 1 to n, the correct position in the array of element arr[i] should be at index i+1. arr[0] should be 1, arr[1] should be 2 and…….arr[n-1] should be n.

Arr[]= { 4,3,2,1 }

Minimum move-to-front operations: 3

Pull 3, 3,4,2,1 count=1 Pull 2, 2,3,4,1 count=2 Pull 1, 1,2,3,4 count=3

Arr[]= { 6,1,2,5,4,3 }

Minimum move-to-front operations: 5

Pull 5, 5,6,1,2,4,3 count=1 Pull 4, 4,5,6,1,2,3 count=2 Pull 3, ,4,5,6,1,2 count=3 Pull 2, 2,3,4,5,6,1 count=4 Pull 1, 1,2,3,4,5,6 count=5

Integer array Arr[] stores the numbers 1 to n.

Integer variable size stores the length of array Arr[]

Function movetoFront(int arr[], int n) takes an array and its length as input and returns the minimum number of ‘move-to-front’ operations required to sort that given array.

Count variable is initialized with size of array as all elements can be moved in case of decreasing order array.

Start traversing from the last index and moving towards the front, if element value is the same as count, (for sorted elements between 1 to n, n is last, n-1 is second last and so on) then decrement count as that element is at the correct position.

In this way at the end of the loop, count has the desired result.

#include <bits/stdc++.h> using namespace std; // Calculate minimum number of moves to arrange array // in increasing order. int movetoFront(int arr[], int n){ //take count as all elements are correctly placed int count = n; // Traverse array from end for (int i=n-1; i >= 0; i--){ // If current item is at its correct position, //decrement the count //range is 1 to n so every arr[i] should have value i+1 //1 at 0 index, 2 at 1 index........ if (arr[i] == count) count--; } return count; } int main(){ int Arr[] = {5, 3, 4, 7, 2, 6, 1}; int size = 7; cout <<"Minimum 'move-to-front' to sort array:"<< movetoFront(Arr, size); return 0; }

Minimum 'move-to-front' to sort array:6

- Related Questions & Answers
- Minimum number of swaps required to sort an array in C++
- Minimum Moves to Equal Array Elements in C++
- Find the minimum number of moves needed to move from one cell of matrix to another in Python
- Count number of 1s in the array after N moves in C
- Minimum number of moves to make all elements equal using C++.
- Sort an array according to count of set bits in C++
- Minimum number of moves to escape maze matrix in Python
- How to front pad zip code with “0” in MySQL?
- C# program to count number of bytes in an array
- Move last element to front of a given Linked List in C++
- Move all zeros to the front of the linked list in C++
- Minimum Knight Moves in C++
- How to “return an object” in C++?
- Minimum Moves to Equal Array Elements II in Python
- Minimum value of “max + min” in a subarray in C++

Advertisements