Minimum Sub Array to Sort

 Q91Given an unsorted array of specific size. Write a program in C to find the minimum length of subarray such that, sorting this subarray makes the whole array sorted.

Expected 

The given array is:
10 12 15 17 28 32 42 18 56 59 67
The minimum length of unsorted subarray which makes the given array sorted
lies between the index 4 and 7

Solution : 

#include<iostream>

using namespace std;

int main(){

    int n;

    cin>>n;

    int arr[n];

    for(int i=0;i<n;i++){

        cin>>arr[i];

    }

    int li=n,ri=0;

    for(int i=0;i<n-1;i++){

        if(arr[i]>arr[i+1]){

            cout<<"Get disorder "<<i+1<<endl;

            ri=i+1;

            int lli=0;

            for(int j=i-1;j>=0;j--){

                if(arr[j]<arr[ri]){

                    cout<<"\t less get "<<arr[j]<<' '<<arr[ri]<<endl;

                    lli=j+1;

                    break;

                }else{

                    cout<<"\t not les "<<arr[j]<<' '<<arr[ri]<<endl;

                }

            }if(lli<li) li=lli;

        }else{

            cout<<"ordered : "<<arr[i]<<' '<<arr[i+1]<<endl;

        }

    }cout<<li<<' '<<ri<<endl;

}

/*
Input:
11
10 12 15 17 28 32 42 18 56 59 67
Output:
4 7
Input:
11
10 12 15 17 28 32 42 18 56 11 59
Output
1 9
Input:
11
10 12 15 17 28 32 42 18 56 30 59
Output:
4 9

Worst case : 
9
9 8 7 6 5 4 3 2 1
Output :
0 8
*/

মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

Big Big mod

শিরোনামহীন গল্প

Dictionaries and Maps