Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

min n-m so that whole array will be sorted

Tags:

java

algorithm

I was asked the below question in an interview:

Given an array of integers, write a method to find indices m and n such that 
if you sorted elements m through n, the entire array would be sorted. Minimize
n-m. i.e. find smallest sequence.

find my answer below and please do comment on the solution. Thanks!!!

like image 931
Trying Avatar asked Apr 06 '13 20:04

Trying


People also ask

What is the minimum number of operations required to sort the array in ascending order?

Therefore, the minimum moves required is 2.

What is almost sorted array?

An array is said to be almost sorted (non-decreasing) if any of its elements can occur at a maximum of 1 distance away from their original places in the sorted array.

Can array elements be sorted?

Sorting array of objectsArrays of objects can be sorted by comparing the value of one of their properties.

What is the minimum number of iterations in a binary search algorithm if the key is the middle element of the array?

In the best case, where the target value is the middle element of the array, its position is returned after one iteration.


1 Answers

At last I have got a solution to the problem, please feel free to comment.

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: enter image description here

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:

enter image description here

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. Please provide your valuable inputs. I tried to explain to my label best, please let me know if i want to explain more. Waiting for valuable inputs.

The below code implements the above logic:

public void getMN(int[] a)
{
    int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE;
    for(int i=1; i<a.length; i++)
    {
        if(a[i]<a[i-1])
        {
            if(a[i-1] > max)
            {
                max = a[i-1];
            }
            if(a[i] < min)
            {
                min = a[i];
            }
        }
    }
    if(max == Integer.MIN_VALUE){System.out.println("Array already sorted!!!");}
    int m =-1, n =-1;
    for(int i=0; i<a.length; i++)
    {
        if(a[i]<=min)
        {
            m++;
        }
        else
        {
            m++;
            break;
        }
    }
    for(int i=a.length-1; i>=0; i--)
    {
        if(a[i]>=max)
        {
            n++;
        }
        else
        {
            n++;
            break;
        }
    }
    System.out.println(m +" : "+(a.length-1-n));
    System.out.println(min +" : "+max);
}
like image 154
Trying Avatar answered Sep 28 '22 16:09

Trying