3

Q I've been trying to solve. Given an array : 1 2 2 4 4 6 5 4 5 7 8 9 11 13 Find the first element that is greater than all previous elements and smaller than all elements ahead. My thinking was to sort the array and then find the first element that had not changed its original position in the array. What do you think? Anyone has a better approach ?

Is there a way to do it in less than O(N^2) ?

thanks

16
  • 1
    I'm not sure if your approach would work. Because let's say you have the array [5, 7, 7, 2], it's possible that the second 7 in the array does not move during the sort, but it does not fulfit the condition, Commented May 4, 2014 at 8:53
  • 1
    Oops.. my bad, sorry. Commented May 4, 2014 at 8:53
  • 1
    @zouZou: but he is getting the first element of all not moving elements Commented May 4, 2014 at 8:55
  • 1
    @lakshman And the first not moving element would be the second 7 if the sort behaves like I describe, and thus an invalid answer. Commented May 4, 2014 at 9:01
  • 1
    @MarounMaroun For that it requires that you have to perform a check before. The solution "sort it and then check the first not moving position" is not working. Commented May 4, 2014 at 9:02

3 Answers 3

7
  1. Sweep from left to right, tracking current maximum. Mark any element that is the current maximum.
  2. Sweep from right to left, tracking current minimum. Also track the current leftmost marked element that is also the current minimum.

(With thanks to @Konstantinos for the suggested optimisations in comments below.)

Step 1 finds all elements that satisfy the first criterion, similarly for Step 2. The whole thing is O(n) in time and space.

Sign up to request clarification or add additional context in comments.

Comments

0

According to @Oli's solution:

public static void getElement(int[] array) {
    int n = array.length;
    boolean[] maxis = new boolean[n];
    //compute max
    int max = array[0];
    maxis[0] = true;
    for (int i = 1; i < n; i++) {
        if (array[i] > max) {
            max = array[i];
            maxis[i] = true;
        }
    }
    //initialize requestedNumber, if we get -1 as a reply there is no solution
    int requestedPos = -1;
    int requestedNumber = 0;


    int min = array[n-1];
    if (maxis[n-1]) {
        requestedPos = n-1;
        requestedNumber = min;
    }

    //compute min
    //keep track of current requestedNumber
    for (int i = n - 1 ; i >= 0; i--) {
        if (array[i] < min) {
            min = array[i];
            if (maxis[i]) {
                requestedPos = i;
                requestedNumber = min;
            }
        }
    }


    System.out.println(requestedPos);
    System.out.println(requestedNumber);
}

2 Comments

This is cool, but it's generally not a great idea to give complete code answers to homework questions.
yes that's true, almost forgot it was for a homework, due to the run to prove there exist an O(n) algorithm.
0

I think there is an even faster approach without requiring additional arrays or markings.

We just need one full table scan and nothing more, so it's obviously O(n):

public static void getFirstMinMaxElement(int[] array) {
    //compute max
    int max = array[0];
    int requestedPos = 0; //position
    int requestedNumber = max; //current
    for (int i = 1; i < array.length; i++) {
        if (array[i] > max) {
            max = array[i];
            //only assign a new requested value if there isn't any
            if (requestedPos == -1) {
                requestedPos = i;
                requestedNumber = max;
            }
        }
        //delete current requestedNumber when you find a smaller one
        //use < instead of <= if you don't care about equal elements 
        else if (array[i] <= requestedNumber) {
            requestedPos = -1;
        }
    }
    System.out.println(requestedPos); //if -1, then there is no solution
    System.out.println(requestedNumber);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.