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 s be the index of such an element. In the above example 1, s 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 e 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 max. minand 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 s 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 e to index of this element. In the above example 1, e is changed to 8 (index of 35)
3) Print s and e.
Lets take an example:
int a[]={1,3,4,6,10,6,16,12,13,15,16,19,20,22,25}
Now if i will put this in to the graph (X-coordinate -> array index and Y-coordinate -> array's value) then the graph will look like as below:
Now if we see the graph there are two places where dip happens one is after 10 and another after 16. Now in the zig zag portion if we see the min value is 6 and max val is 16. So the portion which we should sort to make the whole array sorted is between (6,16). Please refer to the below image:
Now we can easily divide the array in to three part. And middle part the one which we want to sort so that the whole array will be sorted.
Reference:
http://stackoverflow.com/questions/15855594/min-n-m-so-that-whole-array-will-be-sorted/15855670
相关推荐
Make a 2D RPG in a Weekend shows you how to create your very own dungeon crawler game in RPG Maker VX Ace in a single weekend. The entire process, from start to finish, is covered within this book. ...
### SAP BSP应用下载工具:全面备份MVC结构与相关组件 #### 概述 在SAP实施场景中,Business Server Pages (BSP) 应用程序通常扮演着至关重要的角色。由于其高度的价值贡献,确保在开发阶段能够妥善地保存这些应用...
Agile methods are displacing traditional, process-heavy project management techniques, and are poised to migrate from software development to the rest of IT, and to the entire enterprise. Agile’s ...
整帧丢失相关论文 整帧丢失相关论文 整帧丢失相关论文 aiming at the error concealment of the entire frame loss.
Once you are familiar with Gamesalad, you will be able to prototype a game in a few days and complete the entire project in as little as a few weeks instead of the typical months or years it could ...
This book is both a comprehensive reference of the entire OpenGL API and a tutorial that will teach you how to use this powerful API to create stunning 3D visualizations, games, and other ...
This book is both a comprehensive reference of the entire OpenGL API and a tutorial that will teach you how to use this powerful API to create stunning 3D visualizations, games, and other ...
Printed entirely in color, with helpful figures and syntax coloring to make code samples appear as they do in Visual Studio and Expression Blend! Using Silverlight 4, you can build richer and more ...
Printed entirely in color, with helpful figures and syntax coloring to make code samples appear as they do in Visual Studio and Expression Blend! Using Silverlight 4, you can build richer and more ...
But it is really child's play compared to everything else that a good programmer must do to make a software system that succeeds for both the customer and myriad colleagues for whom he or she is ...
Safari allows you to read entire books online, as well as quickly search the collection for relevant information. Learn more Products included in the SQL Toolbelt: * SQL Compare® Pro: compare ...
have tried, however, to make this a book that will be useful to you now as a course textbook and also later in your career as a mathematical desk reference or an engineering handbook. What are the ...
Oracle RMAN for Absolute Beginners is a gentle introduction to the use of Oracle's Recovery Manager software to make backups of an Oracle database, and to restore all or part of a database in the ...
Meanwhile, the entire LO network is also locked to a 75-MHz reference, facilitating phase-coherent pairing with external sub-terahertz transmitters. A chip prototype using a bulk 65-nm CMOS ...
Today, our entire modern way of life, from communication to commerce to conflict, fundamentally depends on the Internet. And the cybersecurity issues that result challenge literally everyone: ...
In this collection of essays and articles, key members of Google’s Site Reliability Team explain how and why their commitment to the entire lifecycle has enabled the company to successfully build, ...
The Executive’s How-To Guide to Automation: Mastering AI and Algorithm-Driven Business By 作者: George E. Danner ISBN-10 书号: 3319997882 ISBN-13 书号: 9783319997889 Edition 版本: 1st ed. 2019 出版...
Folder conversion: Entire folders of C# code files are converted to Java code files.(文件夹转换) 2. Code snippet and file conversion: Our snippet conversion accuracy is outstanding and does not ...