Number of subarrays required to be rearranged to sort the given array
Last Updated :
05 Jan, 2022
Given an array arr[] consisting of the first N natural numbers, the task is to find the minimum number of subarrays required to be rearranged such that the resultant array is sorted.
Examples:
Input: arr[] = {2, 1, 4, 3, 5}
Output: 1
Explanation:
Operation 1: Choose the subarray {arr[0], arr[3]}, i.e. { 2, 1, 4, 3 }. Rearrange the elements of this subarray to {1, 2, 3, 4}. The array modifies to {1, 2, 3, 4, 5}.
Input: arr[] = {5, 2, 3, 4, 1}
Output: 3
Approach: The given problem can be solved by observing the following scenarios:
- If the given array arr[] is already sorted, then print 0.
- If the first and the last element is 1 and N respectively, then only 1 subarray either arr[1, N – 2] or arr[2, N – 1] needs to be sorted. Therefore, print 1.
- f the first and the last element is N and 1 respectively, then 3 subarrays i.e., arr[0, N – 2], arr[1, N – 1], and arr[0, 1] need to be sorted. Therefore, print 3.
- Otherwise, sort the two subarrays i.e., arr[1, N – 1], and arr[0, N – 2].
Therefore, print the count of minimum number of subarrays required to be rearranged.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countSubarray( int arr[], int n)
{
int ans = 2;
if (is_sorted(arr, arr + n)) {
ans = 0;
}
else if (arr[0] == 1
|| arr[n - 1] == n) {
ans = 1;
}
else if (arr[0] == n
&& arr[n - 1] == 1) {
ans = 3;
}
cout << ans;
}
int main()
{
int arr[] = { 5, 2, 3, 4, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
countSubarray(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int arraySortedOrNot( int arr[], int n)
{
if (n == 1 || n == 0 )
return 1 ;
if (arr[n - 1 ] < arr[n - 2 ])
return 0 ;
return arraySortedOrNot(arr, n - 1 );
}
static void countSubarray( int arr[], int n)
{
int ans = 2 ;
if (arraySortedOrNot(arr, arr.length) != 0 )
{
ans = 0 ;
}
else if (arr[ 0 ] == 1 ||
arr[n - 1 ] == n)
{
ans = 1 ;
}
else if (arr[ 0 ] == n &&
arr[n - 1 ] == 1 )
{
ans = 3 ;
}
System.out.print(ans);
}
public static void main(String[] args)
{
int arr[] = { 5 , 2 , 3 , 4 , 1 };
int N = arr.length;
countSubarray(arr, N);
}
}
|
Python3
def countSubarray(arr, n):
ans = 2
if ( sorted (arr) = = arr):
ans = 0
elif (arr[ 0 ] = = 1 or arr[n - 1 ] = = n):
ans = 1
elif (arr[ 0 ] = = n and arr[n - 1 ] = = 1 ):
ans = 3
print (ans)
arr = [ 5 , 2 , 3 , 4 , 1 ]
N = len (arr)
countSubarray(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int arraySortedOrNot( int []arr, int n)
{
if (n == 1 || n == 0)
return 1;
if (arr[n - 1] < arr[n - 2])
return 0;
return arraySortedOrNot(arr, n - 1);
}
static void countSubarray( int []arr, int n)
{
int ans = 2;
if (arraySortedOrNot(arr, arr.Length) != 0)
{
ans = 0;
}
else if (arr[0] == 1 ||
arr[n - 1] == n)
{
ans = 1;
}
else if (arr[0] == n &&
arr[n - 1] == 1)
{
ans = 3;
}
Console.Write(ans);
}
public static void Main()
{
int []arr = { 5, 2, 3, 4, 1 };
int N = arr.Length;
countSubarray(arr, N);
}
}
|
Javascript
<script>
function arraySortedOrNot(arr, n)
{
if (n == 1 || n == 0)
return 1;
if (arr[n - 1] < arr[n - 2])
return 0;
return arraySortedOrNot(arr, n - 1);
}
function countSubarray(arr, n)
{
var ans = 2;
if (arraySortedOrNot(arr, arr.length) != 0)
{
ans = 0;
}
else if (arr[0] == 1 ||
arr[n - 1] == n)
{
ans = 1;
}
else if (arr[0] == n &&
arr[n - 1] == 1)
{
ans = 3;
}
document.write(ans);
}
var arr = [ 5, 2, 3, 4, 1 ];
var N = arr.length;
countSubarray(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...