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);
}
[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,