需要重新排列以对给定数组进行排序的子数组的数量
给定一个由前N个自然数组成的数组arr[] ,任务是找到需要重新排列的最小子数组数,以便对结果数组进行排序。
例子:
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
方法:给定的问题可以通过观察以下场景来解决:
- 如果给定的数组arr[]已经排序,则打印0 。
- 如果第一个和最后一个元素分别为1和N ,则只需对arr[1, N – 2]或arr[2, N – 1]中的 1 个子数组进行排序。因此,打印1 。
- 如果第一个和最后一个元素分别为N和1 ,则需要对 3 个子数组arr[0, N – 2] 、 arr[1, N – 1]和arr[0, 1]进行排序。因此,打印3 。
- 否则,对两个子数组进行排序,即arr[1, N – 1]和arr[0, N – 2] 。
因此,打印需要重新排列的最小子数组数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出:
3
时间复杂度: O(N)
辅助空间: O(1)