Sort Permutation of 1 to N by removing any element and inserting it to front or back
Last Updated :
03 May, 2022
Given an array arr[] of size N having distinct integers from 1 to N, the task is to count the minimum number of steps required to sort the array in increasing order by removing any element and inserting it to the front or back of the array.
Examples:
Input: arr[ ] = {4, 1, 3, 2}
Output: 2
Explanation:
The given array can be sorted in increasing order by following two operations:
Operation 1: Remove 3 and add it to the back of the array. {4, 1, 3, 2} -> {4, 1, 2, 3}
Operation 2: Remove 4 and add it to the back of the array. {4, 1, 2, 3} -> {1, 2, 3, 4}
Input: arr[ ] = {4, 1, 2, 5, 3}
Output: 2
Approach: The problem can be solved by using the fact that to make the array sorted in a minimum number of steps, the minimum number of elements must be moved to the front or back. Also, the elements whose position must be changed will not lie in the increasing order initially. So the problem reduces to finding the longest increasing subarray as only those elements of the array will not be moved.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void findMinStepstoSort( int arr[], int N){
map< int , int > pos;
for ( int i = 0; i < N; i++)
pos[arr[i]] = i;
int ans = N-1;
int prev = -1;
int count = 0;
for ( int i = 1; i < N + 1; i++){
if (pos[i] > prev)
count += 1;
else
count = 1;
prev = pos[i];
ans = min(ans, N - count);
}
cout<<ans;
}
int main(){
int N = 5;
int arr[] = {4, 1, 2, 5, 3};
findMinStepstoSort(arr, N);
}
|
Java
import java.util.*;
class GFG{
static void findMinStepstoSort( int arr[], int N){
HashMap<Integer,Integer> pos = new HashMap<>();
for ( int i = 0 ; i < N; i++)
pos.put(arr[i], i);
int ans = N - 1 ;
int prev = - 1 ;
int count = 0 ;
for ( int i = 1 ; i < N + 1 ; i++){
if (pos.get(i) > prev)
count += 1 ;
else
count = 1 ;
prev = pos.get(i);
ans = Math.min(ans, N - count);
}
System.out.print(ans);
}
public static void main(String[] args){
int N = 5 ;
int arr[] = { 4 , 1 , 2 , 5 , 3 };
findMinStepstoSort(arr, N);
}
}
|
Python3
def findMinStepstoSort(arr, N):
pos = {arr[i]:i for i in range (N)}
ans = N - 1
prev = - 1 ;count = 0
for i in range ( 1 , N + 1 ):
if pos[i] > prev:
count + = 1
else :
count = 1
prev = pos[i]
ans = min (ans, N - count)
print (ans)
if __name__ = = '__main__' :
N = 5
arr = [ 4 , 1 , 2 , 5 , 3 ]
findMinStepstoSort(arr, N)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void findMinStepstoSort( int [] arr, int N){
Dictionary< int , int > pos = new
Dictionary< int , int >();
for ( int i = 0; i < N; i++)
pos[arr[i]] = i;
int ans = N - 1;
int prev = -1;
int count = 0;
for ( int i = 1; i < N + 1; i++){
if (pos[i] > prev)
count += 1;
else
count = 1;
prev = pos[i];
ans = Math.Min(ans, N - count);
}
Console.WriteLine(ans);
}
public static void Main ( string [] args)
{
int N = 5;
int [] arr = {4, 1, 2, 5, 3};
findMinStepstoSort(arr, N);
}
}
|
Javascript
<script>
function findMinStepstoSort(arr, N) {
let pos = new Array(N);
for (let i = 0; i < N; i++) {
pos[i] = arr[i];
}
ans = N - 1
prev = -1; count = 0
for (let i = 1; i < N + 1; i++) {
if (pos[i] > prev)
count += 1
else
count = 1
prev = pos[i]
ans = Math.min(ans, N - count)
}
document.write(ans)
}
let N = 5
let arr = [4, 1, 2, 5, 3]
findMinStepstoSort(arr, N)
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...