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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the number
// of subarrays required to be
// rearranged to sort the given array
void countSubarray(int arr[], int n)
{
// Base Case
int ans = 2;
// Check if the given array is
// already sorted
if (is_sorted(arr, arr + n)) {
ans = 0;
}
// Check if the first element of
// array is 1 or last element is
// equal to size of array
else if (arr[0] == 1
|| arr[n - 1] == n) {
ans = 1;
}
else if (arr[0] == n
&& arr[n - 1] == 1) {
ans = 3;
}
// Print the required answer
cout << ans;
}
// Driver Code
int main()
{
int arr[] = { 5, 2, 3, 4, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
countSubarray(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function that returns 0 if a pair
// is found unsorted
static int arraySortedOrNot(int arr[], int n)
{
// Array has one or no element or the
// rest are already checked and approved.
if (n == 1 || n == 0)
return 1;
// Unsorted pair found (Equal values allowed)
if (arr[n - 1] < arr[n - 2])
return 0;
// Last pair was sorted
// Keep on checking
return arraySortedOrNot(arr, n - 1);
}
// Function to count the number
// of subarrays required to be
// rearranged to sort the given array
static void countSubarray(int arr[], int n)
{
// Base Case
int ans = 2;
// Check if the given array is
// already sorted
if (arraySortedOrNot(arr, arr.length) != 0)
{
ans = 0;
}
// Check if the first element of
// array is 1 or last element is
// equal to size of array
else if (arr[0] == 1 ||
arr[n - 1] == n)
{
ans = 1;
}
else if (arr[0] == n &&
arr[n - 1] == 1)
{
ans = 3;
}
// Print the required answer
System.out.print(ans);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 5, 2, 3, 4, 1 };
int N = arr.length;
countSubarray(arr, N);
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program for the above approach
# Function to count the number
# of subarrays required to be
# rearranged to sort the given array
def countSubarray(arr, n):
# Base Case
ans = 2
# Check if the given array is
# already sorted
if (sorted(arr) == arr):
ans = 0
# Check if the first element of
# array is 1 or last element is
# equal to size of array
elif (arr[0] == 1 or arr[n - 1] == n):
ans = 1
elif (arr[0] == n and arr[n - 1] == 1):
ans = 3
# Print the required answer
print(ans)
# Driver Code
arr = [ 5, 2, 3, 4, 1 ]
N = len(arr)
countSubarray(arr, N)
# This code is contributed by amreshkumar3
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function that returns 0 if a pair
// is found unsorted
static int arraySortedOrNot(int []arr, int n)
{
// Array has one or no element or the
// rest are already checked and approved.
if (n == 1 || n == 0)
return 1;
// Unsorted pair found (Equal values allowed)
if (arr[n - 1] < arr[n - 2])
return 0;
// Last pair was sorted
// Keep on checking
return arraySortedOrNot(arr, n - 1);
}
// Function to count the number
// of subarrays required to be
// rearranged to sort the given array
static void countSubarray(int []arr, int n)
{
// Base Case
int ans = 2;
// Check if the given array is
// already sorted
if (arraySortedOrNot(arr, arr.Length) != 0)
{
ans = 0;
}
// Check if the first element of
// array is 1 or last element is
// equal to size of array
else if (arr[0] == 1 ||
arr[n - 1] == n)
{
ans = 1;
}
else if (arr[0] == n &&
arr[n - 1] == 1)
{
ans = 3;
}
// Print the required answer
Console.Write(ans);
}
// Driver Code
public static void Main()
{
int []arr = { 5, 2, 3, 4, 1 };
int N = arr.Length;
countSubarray(arr, N);
}
}
// This code is contributed by bgangwar59
JavaScript
<script>
// Javascript program for the above approach
// Function that returns 0 if a pair
// is found unsorted
function arraySortedOrNot(arr, n)
{
// Array has one or no element or the
// rest are already checked and approved.
if (n == 1 || n == 0)
return 1;
// Unsorted pair found (Equal values allowed)
if (arr[n - 1] < arr[n - 2])
return 0;
// Last pair was sorted
// Keep on checking
return arraySortedOrNot(arr, n - 1);
}
// Function to count the number
// of subarrays required to be
// rearranged to sort the given array
function countSubarray(arr, n)
{
// Base Case
var ans = 2;
// Check if the given array is
// already sorted
if (arraySortedOrNot(arr, arr.length) != 0)
{
ans = 0;
}
// Check if the first element of
// array is 1 or last element is
// equal to size of array
else if (arr[0] == 1 ||
arr[n - 1] == n)
{
ans = 1;
}
else if (arr[0] == n &&
arr[n - 1] == 1)
{
ans = 3;
}
// Print the required answer
document.write(ans);
}
// Driver Code
var arr = [ 5, 2, 3, 4, 1 ];
var N = arr.length;
countSubarray(arr, N);
// This code is contributed by SURENDRA_GANGWAR
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Smallest Subarray to be Sorted to make the whole array sorted Given an unsorted array arr[]. Find the subarray arr[s...e] such that sorting this subarray makes the whole array sorted.Note: If the given array is already sorted then return [0, 0].Examples:Input: arr[] = [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60]Output: [3, 8]Explanation: Sorting subarray start
15+ min read
Length of smallest subarray to be removed such that the remaining array is sorted Given an array arr[] consisting of N integers, the task is to print the length of the smallest subarray to be removed from arr[] such that the remaining array is sorted. Examples: Input: arr[] = {1, 2, 3, 10, 4, 2, 3, 5}Output: 3Explanation:The smallest subarray to be remove is {10, 4, 2} of length
8 min read
Minimum number of jumps required to Sort the given Array in ascending order| Set-2 Given two arrays arr[] and jump[], each of length N, where jump[i] denotes the number of indices by which the ith element in the array arr[] can move forward, the task is to find the minimum number of jumps required such that the array is sorted in ascending order. All elements of the array arr[] ar
6 min read
Minimum number of swaps required to sort an array | Set 2 Given an array of N distinct elements, find the minimum number of swaps required to sort the array. Note: The problem is not asking to sort the array by the minimum number of swaps. The problem is to find the minimum swaps in which the array can be sorted. Examples: Input: arr[] = {4, 3, 2, 1} Outpu
7 min read
Rearrange array to make sum of all subarrays starting from first index non-zero Given an array arr[] consisting of N integers, the task is to rearrange the array such that sum of all subarrays starting from the first index of the array is non-zero. If it is not possible to generate such arrangement, then print "-1". Examples: Input: arr[] = {-1, 1, -2, 3}Output: {-1, -2, 1, 3}E
12 min read