Problem

Given an integer array nums containing distinct positive integers, find and return any number from the array that is neither the minimum nor the maximum value in the array, or -1 if there is no such number.

Return the selected integer.

Examples

Example 1

1
2
3
Input: nums = [3,2,1,4]
Output: 2
Explanation: In this example, the minimum value is 1 and the maximum value is 4. Therefore, either 2 or 3 can be valid answers.

Example 2

1
2
3
Input: nums = [1,2]
Output: -1
Explanation: Since there is no number in nums that is neither the maximum nor the minimum, we cannot select a number that satisfies the given condition. Therefore, there is no answer.

Example 3

1
2
3
Input: nums = [2,1,3]
Output: 2
Explanation: Since 2 is neither the maximum nor the minimum value in nums, it is the only valid answer. 

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100
  • All values in nums are distinct

Solution

Method 1 -

Intuition

We need to return any value from the array that is neither the minimum nor the maximum. If the array has only two elements, return -1.

Approach

Find the minimum and maximum values in the array. Return any value that is not equal to either. If no such value exists, return -1.

Code

1
2
3
4
5
6
7
int findNonMinOrMax(vector<int>& nums) {
    if (nums.size() < 3) return -1;
    int mn = *min_element(nums.begin(), nums.end());
    int mx = *max_element(nums.begin(), nums.end());
    for (int x : nums) if (x != mn && x != mx) return x;
    return -1;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func findNonMinOrMax(nums []int) int {
    if len(nums) < 3 { return -1 }
    mn, mx := nums[0], nums[0]
    for _, x := range nums {
        if x < mn { mn = x }
        if x > mx { mx = x }
    }
    for _, x := range nums {
        if x != mn && x != mx { return x }
    }
    return -1
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public int findNonMinOrMax(int[] nums) {
    if (nums.length < 3) return -1;
    int mn = nums[0], mx = nums[0];
    for (int x : nums) {
        mn = Math.min(mn, x);
        mx = Math.max(mx, x);
    }
    for (int x : nums) if (x != mn && x != mx) return x;
    return -1;
}
1
2
3
4
5
6
7
fun findNonMinOrMax(nums: IntArray): Int {
    if (nums.size < 3) return -1
    val mn = nums.minOrNull()!!
    val mx = nums.maxOrNull()!!
    for (x in nums) if (x != mn && x != mx) return x
    return -1
}
1
2
3
4
5
6
7
8
def findNonMinOrMax(nums):
    if len(nums) < 3:
        return -1
    mn, mx = min(nums), max(nums)
    for x in nums:
        if x != mn and x != mx:
            return x
    return -1
1
2
3
4
5
6
7
8
9
fn find_non_min_or_max(nums: Vec<i32>) -> i32 {
    if nums.len() < 3 { return -1; }
    let mn = *nums.iter().min().unwrap();
    let mx = *nums.iter().max().unwrap();
    for &x in &nums {
        if x != mn && x != mx { return x; }
    }
    -1
}
1
2
3
4
5
6
function findNonMinOrMax(nums: number[]): number {
    if (nums.length < 3) return -1;
    const mn = Math.min(...nums), mx = Math.max(...nums);
    for (const x of nums) if (x !== mn && x !== mx) return x;
    return -1;
}

Complexity

  • ⏰ Time complexity: O(n) where n is the length of nums.
  • 🧺 Space complexity: O(1).