
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count minimum number of āmove-to-frontā moves to sort an array in C++
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.
Input
Arr[]= { 4,3,2,1 }
Output
Minimum move-to-front operations: 3
Explanation
Pull 3, 3,4,2,1 count=1 Pull 2, 2,3,4,1 count=2 Pull 1, 1,2,3,4 count=3
Input
Arr[]= { 6,1,2,5,4,3 }
Output
Minimum move-to-front operations: 5
Explanation
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
Approach used in the below program is as follows
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.
Example
#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; }
Output
Minimum 'move-to-front' to sort array:6
- Related Articles
- Minimum number of swaps required to sort an array in C++
- Find the minimum number of moves needed to move from one cell of matrix to another in Python
- Minimum Moves to Equal Array Elements in C++
- Sort an array according to count of set bits in C++
- Minimum number of moves to escape maze matrix in Python
- Minimum Moves to Equal Array Elements II in Python
- Count number of 1s in the array after N moves in C
- Program to find minimum moves to make array complementary in Python
- C# program to count number of bytes in an array
- Minimum number of moves to make all elements equal using C++.
- C/C++ Program to Count Inversions in an array using Merge Sort?
- Count minimum right flips to set all values in an array in C++
- Program to find out the number of shifts required to sort an array using insertion sort in python
- Python program to move spaces to front of string in single traversal
- C/C++ Program to the Count Inversions in an array using Merge Sort?
