📌  相关文章
📜  通过拆分为子数组对数组进行排序,其中每个元素仅属于子数组

📅  最后修改于: 2022-05-13 01:56:09.247000             🧑  作者: Mango

通过拆分为子数组对数组进行排序,其中每个元素仅属于子数组

给定一个包含N不同整数的数组arr[] ,任务是检查是否可以通过按顺序执行以下操作来按升序对数组进行排序

  • 将数组arr[]拆分恰好Y(1 <= Y <= N)的非空子数组,使得每个元素恰好属于一个子数组。
  • 以任意顺序重新排序子数组。
  • 合并重新排序的子数组。

例子:

方法:主要思想是,如果对给定数组arr[]进行排序所需的最小拆分次数小于或等于Y,则始终可以对数组进行排序。此外,所需的分割数必须等于将数组划分为最小数量的非递减子数组所需的最小分割数的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Initialization of pair
pair a[100001];
 
// Function to check if it is possible
// to sort the array by performing the
// operations exactly once in order
void checkForSortedSubarray(int n, int y, int arr[])
{
 
    // Traversing the array
    for (int i = 1; i <= n; i++) {
        // Storing the array element
        // in the first part of pair
        a[i].first = arr[i];
 
        // Storing the index as second
        // element in the pair
        a[i].second = i;
    }
 
    // Initialize Count
    int cnt = 1;
 
    // Sorting the array
    sort(a + 1, a + n + 1);
 
    for (int i = 1; i <= n - 1; i++) {
 
        // Checking if the index lies
        // in order
        if (a[i].second != a[i + 1].second - 1)
            cnt++;
    }
 
    // If minimum splits required is
    // greater than y
    if (cnt > y)
        cout << "No";
    else
        cout << "Yes";
}
 
// Driver Code
int main()
 
{
    int Y = 4;
    int arr[] = { 6, 3, 4, 2, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    checkForSortedSubarray(N, Y, arr);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Initialization of pair
static pair []a = new pair[100001];
static class pair
{
    int first, second;
    public pair(int first, int second) 
    {
        this.first = first;
        this.second = second;
    }   
}
// Function to check if it is possible
// to sort the array by performing the
// operations exactly once in order
static void checkForSortedSubarray(int n, int y, int arr[])
{
 
    // Traversing the array
    for (int i = 1; i <= n; i++) {
        // Storing the array element
        // in the first part of pair
        a[i].first = arr[i];
 
        // Storing the index as second
        // element in the pair
        a[i].second = i;
    }
 
    // Initialize Count
    int cnt = 1;
 
    // Sorting the array
    Arrays.sort(a,(a, b) -> a.first - b.first);
 
    for (int i = 1; i <= n - 1; i++) {
 
        // Checking if the index lies
        // in order
        if (a[i].second != a[i + 1].second - 1)
            cnt++;
    }
 
    // If minimum splits required is
    // greater than y
    if (cnt > y)
        System.out.print("No");
    else
        System.out.print("Yes");
}
 
// Driver Code
public static void main(String[] args)
 
{
    int Y = 4;
    int arr[] = { 6, 3, 4, 2, 1 };
    int N = arr.length;
    for(int i = 0;i


Python3
# Python 3 program for the above approach
 
# Initialization of pair
 
# Function to check if it is possible
# to sort the array by performing the
# operations exactly once in order
def checkForSortedSubarray(n, y, arr, a):
   
    # Traversing the array
    for i in range(0, n, 1):
       
        # Storing the array element
        # in the first part of pair
        a[i][0] = arr[i]
 
        # Storing the index as second
        # element in the pair
        a[i][1] = i
 
    # Initialize Count
    cnt = 1
 
    # Sorting the array
    a.sort()
 
    for i in range(0,n,1):
        # Checking if the index lies
        # in order
        if (a[i][1] != a[i + 1][1] - 1):
            cnt += 1
 
    # If minimum splits required is
    # greater than y
    if (cnt > y):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
    Y = 4
    a = [[0,0] for i in range(100001)]
    arr = [6, 3, 4, 2, 1]
    N = len(arr)
    checkForSortedSubarray(N, Y, arr,a)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG {
 
  // Initialization of pair
  static List a = new List();
 
  public class pair {
    public int first, second;
 
    public pair(int first, int second) {
      this.first = first;
      this.second = second;
    }
  }
 
  // Function to check if it is possible
  // to sort the array by performing the
  // operations exactly once in order
  static void checkForSortedSubarray(int n, int y, int []arr) {
 
    // Traversing the array
    for (int i = 1; i <= n; i++)
    {
 
      // Storing the array element
      // in the first part of pair
      a[i].first = arr[i];
 
      // Storing the index as second
      // element in the pair
      a[i].second = i;
    }
 
    // Initialize Count
    int cnt = 1;
 
    // Sorting the array
    a.Sort((c, b) => c.first - b.first);
 
    for (int i = 1; i <= n - 1; i++) {
 
      // Checking if the index lies
      // in order
      if (a[i].second != a[i + 1].second - 1)
        cnt++;
    }
 
    // If minimum splits required is
    // greater than y
    if (cnt > y)
      Console.Write("No");
    else
      Console.Write("Yes");
  }
 
  // Driver Code
  public static void Main(String[] args)
 
  {
    int Y = 4;
    int []arr = { 6, 3, 4, 2, 1 };
    int N = arr.Length;
    for (int i = 0; i < 100001; i++) {
      a.Add(new pair(0, 0));
    }
    checkForSortedSubarray(N - 1, Y, arr);
 
  }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出
Yes

时间复杂度: O(N Log N)
辅助空间: O(N)