Given an array arr[] consisting of N integers, the task is to find the minimum length of the prefix needed to be deleted such that the remaining array elements can be rearranged repeatedly by selecting the first or the last element one by one to form a sorted array.
Examples:
Input: arr[] = {6, 5, 4, 3, 4}
Output: 3
Explanation:
To make an array sorted according to the given condition, remove the first 3 elements.
After deletion, the array arr[] is modified to {3, 4}.
From the array arr[], selecting the first elements one by one, i.e. arr[0] -> arr[1], forms a sorted array {3, 4}.
Input: arr[] = {1, 3, 4, 2}
Output: 3
Naive Approach: The simplest approach is to delete all possible lengths of the prefix from the given array and for every prefix, check if a sorted array can be formed from the remaining array elements by removal of those prefixes. If found to be true, then print the minimum length of the prefix deleted.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by observing that the resultant suffix must be of the form arr[1] ? arr[2] ? … ? arr[N – 2] ? arr[N – 1] where N is the length of the remaining array and the suffix is of the maximum length. Follow the steps below:
- Initialize a variable index to N – 1.
- Traverse the array from the end and stop at the point where arr[index – 1] ? arr[index].
- As the iteration proceeds, we keep decreasing the answer.
- After completing all the above steps, print the value of the index which is the minimum length of the prefix that must be deleted.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinLength(vector< int >& arr)
{
int index = ( int )arr.size() - 1;
while (index > 0
&& arr[index] >= arr[index - 1]) {
index--;
}
return index;
}
int main()
{
vector< int > arr = { 7, 8, 5, 0, -1,
-1, 0, 1, 2, 3, 4 };
cout << findMinLength(arr);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static int findMinLength( int [] arr)
{
int index = ( int )arr.length - 1 ;
while (index > 0 && arr[index] >=
arr[index - 1 ])
{
index--;
}
return index;
}
public static void main(String args[])
{
int arr[]= { 7 , 8 , 5 , 0 , - 1 ,
- 1 , 0 , 1 , 2 , 3 , 4 };
int n = arr.length;
System.out.println(findMinLength(arr));
}
}
|
Python3
def findMinLength(arr):
index = len (arr) - 1 ;
while (index > 0 and arr[index] > = arr[index - 1 ]):
index - = 1 ;
return index;
if __name__ = = '__main__' :
arr = [ 7 , 8 , 5 , 0 , - 1 ,
- 1 , 0 , 1 , 2 , 3 , 4 ];
n = len (arr);
print (findMinLength(arr));
|
C#
using System;
class GFG{
static int findMinLength( int [] arr)
{
int index = ( int )arr.Length - 1;
while (index > 0 && arr[index] >=
arr[index - 1])
{
index--;
}
return index;
}
public static void Main(String []args)
{
int []arr = { 7, 8, 5, 0, -1,
-1, 0, 1, 2, 3, 4 };
int n = arr.Length;
Console.WriteLine(findMinLength(arr));
}
}
|
Javascript
<script>
function findMinLength( arr)
{
let index = parseInt(arr.length) - 1;
while (index > 0 && arr[index] >=
arr[index - 1])
{
index--;
}
return index;
}
let arr= [ 7, 8, 5, 0, -1,
-1, 0, 1, 2, 3, 4 ];
let n = arr.length;
document.write(findMinLength(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)