Elements which have at-least two greater elements

Given an array of distinct integers A, find and return
all elements in array which have at-least two greater elements than themselves.

Note: The results should have the order in which they are present in the original array.



Input Format

The only argument given is the integer array A.

Output Format

Return the elements which have at-least two greater elements than themselves.

Constraints

3 <= length of the array <= 100000
-10^9 <= A[i] <= 10^9 

For Example

Input 1:
    A = [1, 2, 3, 4, 5]
Output 1:
    [1, 2, 3]

Input 2:
    A = [5, 17, 100, 11]
Output 2:
    [5, 11]
Solution:

Naive O(n)

public class Solution {
    public int[] solve(int[] A) {
        int n = A.length;
        int maxVal = Integer.MIN_VALUE;
        int maxIndex = -1;
        int sMaxVal = Integer.MIN_VALUE;
        int sMaxIndex = -1;
        for (int i = 0; i < n; i ++) {
            if (A[i] > maxVal) {
                sMaxVal = maxVal;
                sMaxIndex = maxIndex;
                maxVal = A[i];
                maxIndex = i;
            } else if (A[i] > sMaxVal) {
                sMaxVal = A[i];
                sMaxIndex = i;
            }
            
        }
        int[] result = new int[n - 2];
        int p = 0;
        for (int i = 0; i < n; i ++) {
            if (i == maxIndex || i == sMaxIndex) {
                continue;
            }
            result[p ++] = A[i];
        }
        return result;
    }
}