LeetCode Problem Workspace
Neither Minimum nor Maximum
Find a number in an array that is neither the minimum nor maximum value, or return -1 if no such number exists.
2
Topics
5
Code langs
3
Related
Practice Focus
Easy · Array plus Sorting
Answer-first summary
Find a number in an array that is neither the minimum nor maximum value, or return -1 if no such number exists.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array plus Sorting
This problem involves finding a number from an array that is neither the minimum nor maximum. To solve it, sort the array and pick the middle element. If the array is too small (only two numbers), return -1 as no valid number exists.
Problem Statement
You are given an array of distinct positive integers. Your task is to find and return any integer in the array that is neither the minimum nor the maximum value, or return -1 if no such number exists.
For example, if the input array is [3,2,1,4], the minimum value is 1 and the maximum value is 4, so you could return either 2 or 3 as a valid number. However, if the array has only two numbers, no valid answer exists, and you should return -1.
Examples
Example 1
Input: nums = [3,2,1,4]
Output: 2
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
Input: nums = [1,2]
Output: -1
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
Input: nums = [2,1,3]
Output: 2
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 Approach
Sort the Array
Sort the array and simply return the second smallest or second largest number if it exists. This is the most straightforward approach.
Edge Case for Small Arrays
If the array has fewer than three numbers, directly return -1 because there is no number that satisfies the condition of being neither the minimum nor maximum.
Optimal Space and Time Complexity
For large arrays, sorting the array once and accessing the middle element minimizes space complexity, while maintaining reasonable time complexity.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
The time complexity of sorting the array is O(n log n), where n is the number of elements. Space complexity depends on the sorting algorithm used but is typically O(n) for in-place sorts like quicksort.
What Interviewers Usually Probe
- Candidate selects a valid number by sorting and finding an appropriate element.
- Candidate quickly identifies that small arrays require an early return of -1.
- Candidate avoids overcomplicating the solution by using a sorting-based approach.
Common Pitfalls or Variants
Common pitfalls
- Returning the wrong element if the array is not sorted.
- Forgetting the edge case when the array length is 2, which requires returning -1.
- Attempting to solve the problem without sorting the array, leading to incorrect results.
Follow-up variants
- Consider using a non-sorting approach for optimization in large arrays.
- Allow the return of the smallest valid number rather than any number.
- Adjust the problem to handle arrays with repeated numbers.
FAQ
What is the primary pattern for solving this problem?
The primary pattern is array manipulation plus sorting to find a valid number that is neither the minimum nor maximum.
How do I handle small arrays in this problem?
For arrays with fewer than three elements, return -1 as there is no valid number that is neither the minimum nor the maximum.
Why is sorting the array the recommended approach?
Sorting the array allows easy access to the second smallest or second largest element, providing a quick solution.
Can I return any number other than the second smallest or second largest?
No, the solution requires that the number be neither the minimum nor maximum. The second smallest or second largest are the only valid choices.
What if the array contains duplicate numbers?
This problem specifies distinct numbers in the array, so duplicates are not a concern here.
Solution
Solution 1: Simulation
First, we find the minimum and maximum values in the array, denoted as $mi$ and $mx$ respectively. Then, we traverse the array and find the first number that is not equal to $mi$ and not equal to $mx$, and return it.
class Solution:
def findNonMinOrMax(self, nums: List[int]) -> int:
mi, mx = min(nums), max(nums)
return next((x for x in nums if x != mi and x != mx), -1)Solution 2
#### Python3
class Solution:
def findNonMinOrMax(self, nums: List[int]) -> int:
mi, mx = min(nums), max(nums)
return next((x for x in nums if x != mi and x != mx), -1)Continue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array plus Sorting
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward