DEV Community

Piyush Acharya
Piyush Acharya

Posted on

2733. LeetCode's Neither Minimum nor Maximum - JAVA SOLUTION BEATS 100% OF SOLUTIONS IN BOTH RUNTIME AND MEMORY

Code

class Solution {
    public int findNonMinOrMax(int[] nums) {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > max) {
                max = nums[i];
            } if (nums[i] < min) {
                min = nums[i];
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != max && nums[i] != min) {
                return nums[i];
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Solution sol = new Solution();
        int[] nums = { 1, 2 };
        System.out.println(sol.findNonMinOrMax(nums));
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)