Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted

Given an unsorted array arr[0..n-1] of size n, find the minimum length subarray arr[s..e] such that sorting this subarray makes the whole array sorted.

Examples:

1) If the input array is [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60], your program should be able to find that the subarray lies between the indexes 3 and 8.

2) If the input array is [0, 1, 15, 25, 6, 7, 30, 40, 50], your program should be able to find that the subarray lies between the indexes 2 and 5.

Solution:
1) Find the candidate unsorted subarray 
a) Scan from left to right and find the first element which is greater than the next element. Let be the index of such an element. In the above example 1, is 3 (index of 30).
b) Scan from right to left and find the first element (first in right to left order) which is smaller than the next element (next in right to left order). Let be the index of such an element. In the above example 1, e is 7 (index of 31).

2) Check whether sorting the candidate unsorted subarray makes the complete array sorted or not. If not, then include more elements in the subarray.
a) Find the minimum and maximum values in arr[s..e]. Let minimum and maximum values be min and maxminand max for [30, 25, 40, 32, 31] are 25 and 40 respectively.
b) Find the first element (if there is any) in arr[0..s-1] which is greater than min, change to index of this element. There is no such element in above example 1.
c) Find the last element (if there is any) in arr[e+1..n-1] which is smaller than max, change to index of this element. In the above example 1, e is changed to 8 (index of 35)

3) Print and e.

C++ 代码:

Time Complexity: O(n)

#include<stdio.h>

void printUnsorted(int arr[], int n)
{
  int s = 0, e = n-1, i, max, min;   

  // step 1(a) of above algo
  for (s = 0; s < n-1; s++)
  {
    if (arr[s] > arr[s+1])
      break;
  }
  if (s == n-1)
  {
    printf("The complete array is sorted");
    return;
  }

  // step 1(b) of above algo
  for(e = n - 1; e > 0; e--)
  {
    if(arr[e] < arr[e-1])
      break;
  }

  // step 2(a) of above algo
  max = arr[s]; min = arr[s];
  for(i = s + 1; i <= e; i++)
  {
    if(arr[i] > max)
      max = arr[i];
    if(arr[i] < min)
      min = arr[i];
  }

  // step 2(b) of above algo
  for( i = 0; i < s; i++)
  {
    if(arr[i] > min)
    {
      s = i;
      break;
    }
  } 

  // step 2(c) of above algo
  for( i = n -1; i >= e+1; i--)
  {
    if(arr[i] < max)
    {
      e = i;
      break;
    }
  }  

  // step 3 of above algo
  printf(" The unsorted subarray which makes the given array "
         " sorted lies between the indees %d and %d", s, e);
  return;
}

int main()
{
  int arr[] = {10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60};
  int arr_size = sizeof(arr)/sizeof(arr[0]);
  printUnsorted(arr, arr_size);
  getchar();
  return 0;
}

reference:

http://www.geeksforgeeks.org/minimum-length-unsorted-subarray-sorting-which-makes-the-complete-array-sorted/

时间: 2024-10-09 20:50:14

Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted的相关文章

F - The Minimum Length HUST1010( kmp计算最小循环节)

F - The Minimum Length Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit Status Description There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now

D. Subarray Sorting

D. Subarray Sorting 题意:给两个长度为n的数字串,a,b每次可以给a串任意长度区间按非递减排序,问a串是否能变为b串. 思路:一开始写的时候,贪心思路错=.=  看了看别人贪心的思路然后过了,做法是依次枚举b串上的每个数字,然后在a串上依次寻找里b串最近的位置k,然后判断在a串中[1,k]这个区间中,这个数字是不是最小值,如果是ok,否则no,在a中判断成立的数字要去掉.(这个贪心emm,总感觉哪里怪怪的,没法证明) 搞定贪心的思路就好做了,给a串建个线段树,用来查找[1,k

Educational Codeforces Round 67 D. Subarray Sorting

Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得到\(b\)数组. \(n\leq 3*10^5,a\leq n.\) 思路: 首先注意到任意次排序可以等价于任意次交换两个相邻的数,当且仅当前一个数不小于后面一个数. 我一开始想的是按权值从小到大来构造,但最终发现这条路走不通. 正解就是比较直接的思路,按位置一个一个来匹配. 对于一个\(b_i\

F - The Minimum Length

There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the length

HUST - 1010 The Minimum Length

Description There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me

hust1010 The Minimum Length

题目描述 There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the le

The Minimum Length - HUST 1010(求最小循环节)

题意:有个一字符串A(本身不是循环串),然后经过很多次自增变成AAAAA,然后呢从自增串里面切出来一部分串B,用这个串B求出来A的长度.   分析:其实就是求最小循环节.......串的长度 - 最大的匹配. 代码如下. =========================================================================================================== #include<stdio.h> #include<

HUST 1010 The Minimum Length(KMP,最短循环节点)

题意: 有一个字符串A,假设A是“abcdefg”,  由A可以重复组成无线长度的AAAAAAA,即“abcdefgabcdefgabcdefg.....”. 从其中截取一段“abcdefgabcdefgabcdefgabcdefg”,取红色部分为截取部分,设它为字符串B. 现在先给出字符串B, 求A最短的长度. 分析: 设字符串C = AAAAAAAA....  由于C是由无数个A组成的,所以里面有无数个循环的A, 那么从C中的任意一个起点开始,也都可以有一个循环,且这个循环长度和原来的A一样

CF1187D Subarray Sorting

思路: 线段树好题.对a数组中的每个元素从左到右依次操作,判断最终是否能够转化成b数组.在此过程中使用线段树维护区间最小值判断是否能够进行合法操作. 实现: 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 300005, INF = 0x3f3f3f3f; 4 deque<int> d[N]; 5 int a[N], b[N], tree[N * 4]; 6 7 void build(int num