Neither Minimum nor Maximum - 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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [3,2,1,4]
Output: 2
💡 Note: Array has min=1, max=4. Elements 2 and 3 are neither min nor max, so we can return either one. Return 2.
Example 2 — Small Array
$ Input: nums = [1,2]
Output: -1
💡 Note: Array has only 2 elements: min=1 and max=2. No element exists that is neither min nor max.
Example 3 — Larger Array
$ Input: nums = [2,1,3]
Output: 2
💡 Note: Array has min=1, max=3. Element 2 is neither the minimum nor maximum, so return 2.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 100
  • All the numbers of nums are unique.

Visualization

Tap to expand
Neither Minimum nor Maximum: Find the Middle GroundInput Array3214Categorized3214VALIDVALIDMINMAXReturn any valid element: 3 or 2Output: 2 (or 3)
Understanding the Visualization
1
Input
Array with distinct elements [3,2,1,4]
2
Identify Extremes
Find minimum (1) and maximum (4) values
3
Pick Middle
Return any element that's neither min nor max (2 or 3)
Key Takeaway
🎯 Key Insight: Any element that's not an extreme value is a valid answer
Asked in
Google 15 Amazon 12 Microsoft 8
23.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen