careercup-Medium Difficulty 17.6

Source: Internet
Author: User

17.6 given an array of integers, write a function that finds the index m and N, as long as the elements between M and N are sequenced, the entire array is ordered. Note: n the smaller the better, that is, to find the shortest sequence that meets the criteria.

Solution:

Before we begin to solve the problem, let's make sure what the answer will look like. If you are looking for two indexes, this indicates that there is an order in the middle of the array, where the beginning and end of the array are sorted.

Now, let's use the following example to solve this problem:

1,2,4,7,10,11,7,12,6,7,16,18,19

The idea that first came to mind might be to directly find the longest increment subsequence at the beginning, and the longest increment subsequence at the end.

Left: 1,2,4,7,10,11

Middle: 7,12

Right: 6,7,16,18,19

It's easy to find these subsequence, just start with the leftmost and rightmost array, and look in the middle to increment the subsequence. Once the element size is found to be incorrect, the two ends of the increment/decrement sub-sequence are found.

However, in order to solve this problem, we also need to sort the middle part of the array, so long as the middle part is ordered, all the elements of the arrays are ordered. Specifically, it is a judgment condition that must be true:

All elements on the left are smaller than all elements in the middle.

Min (middle) >end (left)

All elements in the middle are smaller than all elements on the right.

Max (middle) <start (right)

Or, in other words, for all elements:

Left<middle<right

In fact, this condition of the above example is absolutely impossible to set up. By definition, the elements of the middle part are unordered. In the example above, Left.end>middle.start and Middle.end>right.start must be established. In this way, sorting only the middle part does not make the entire array orderly.

However, we can also reduce the left and right sub-sequences until the previous conditions have been established.

Make min equal to min (middle), Max equals Max (middle).

On the left, we start at the end of the subsequence and move to the left until we find the element index I so that the array[i]<min; can be sorted only by ordering the middle part, so that the part of the array is ordered.

Then, do something similar to the right part.

C + + Implementation code:

#include <iostream>using namespacestd;intFindendofleftsubsequence (intArray[],intN) {    inti;  for(i=1; i<n;i++)    {        if(array[i]<array[i-1])            returnI-1; }    returnN-1;}intFindendofrightsubsequence (intArray[],intN) {    inti;  for(i=n-2; i>=0; i--)    {        if(array[i]>array[i+1])            returni+1; }    return 0;}intShrinkleft (intArray[],intNintMin_index,intstart) {    intcomp=Array[min_index]; cout<<comp<<Endl; inti;  for(i=start-1; i>=0; i--)    {        if(comp>=Array[i])returni+1; }    return 0;}intShrinkright (intArray[],intNintMax_index,intstart) {    intcomp=Array[max_index]; cout<<comp<<Endl;  for(inti=start;i<n;i++)    {        if(comp<=Array[i])returnI-1; }    returnN-1;}voidFindunsortedsequence (intArray[],intN) {    intend_left=findendofleftsubsequence (array,n); cout<<end_left<<Endl; intstart_right=findendofrightsubsequence (array,n); cout<<start_right<<Endl; intmin_index=end_left+1; if(min_index>=N)return; intmax_index=start_right-1;  for(inti=end_left;i<=start_right;i++)    {        if(array[i]<Array[min_index]) Min_index=i; if(array[i]>Array[max_index]) Max_index=i; } cout<<array[min_index]<<" "<<array[max_index]<<Endl; intright_index=shrinkright (array,n,max_index,start_right); intleft_index=Shrinkleft (Array,n,min_index,end_left); cout<<left_index<<" "<<right_index<<Endl;}intMain () {intarr[ A]={1,2,4,7,Ten,7, A,6,7, -, -, +}; Findunsortedsequence (arr, A);}

careercup-Medium Difficulty 17.6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.