Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
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:
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:
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 <= 1001 <= nums[i] <= 100nums are distinctProblem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[3,2,1,4]
[1,2]
[2,1,3]
third-maximum-number)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2733: Neither Minimum nor Maximum
class Solution {
public int findNonMinOrMax(int[] nums) {
int mi = 100, mx = 0;
for (int x : nums) {
mi = Math.min(mi, x);
mx = Math.max(mx, x);
}
for (int x : nums) {
if (x != mi && x != mx) {
return x;
}
}
return -1;
}
}
// Accepted solution for LeetCode #2733: Neither Minimum nor Maximum
func findNonMinOrMax(nums []int) int {
mi, mx := slices.Min(nums), slices.Max(nums)
for _, x := range nums {
if x != mi && x != mx {
return x
}
}
return -1
}
# Accepted solution for LeetCode #2733: Neither Minimum nor Maximum
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)
// Accepted solution for LeetCode #2733: Neither Minimum nor Maximum
impl Solution {
pub fn find_non_min_or_max(nums: Vec<i32>) -> i32 {
let mut mi = 100;
let mut mx = 0;
for &ele in nums.iter() {
if ele < mi {
mi = ele;
}
if ele > mx {
mx = ele;
}
}
for &ele in nums.iter() {
if ele != mi && ele != mx {
return ele;
}
}
-1
}
}
// Accepted solution for LeetCode #2733: Neither Minimum nor Maximum
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2733: Neither Minimum nor Maximum
// class Solution {
// public int findNonMinOrMax(int[] nums) {
// int mi = 100, mx = 0;
// for (int x : nums) {
// mi = Math.min(mi, x);
// mx = Math.max(mx, x);
// }
// for (int x : nums) {
// if (x != mi && x != mx) {
// return x;
// }
// }
// return -1;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.