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, return the number of elements that have both a strictly smaller and a strictly greater element appear in nums.
Example 1:
Input: nums = [11,7,2,15]
Output: 2
Explanation: The element 7 has the element 2 strictly smaller than it and the element 11 strictly greater than it.
Element 11 has element 7 strictly smaller than it and element 15 strictly greater than it.
In total there are 2 elements having both a strictly smaller and a strictly greater element appear in nums.
Example 2:
Input: nums = [-3,3,3,90]
Output: 2
Explanation: The element 3 has the element -3 strictly smaller than it and the element 90 strictly greater than it.
Since there are two elements with the value 3, in total there are 2 elements having both a strictly smaller and a strictly greater element appear in nums.
Constraints:
1 <= nums.length <= 100-105 <= nums[i] <= 105Problem summary: Given an integer array nums, return the number of elements that have both a strictly smaller and a strictly greater element appear in nums.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[11,7,2,15]
[-3,3,3,90]
find-smallest-letter-greater-than-target)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2148: Count Elements With Strictly Smaller and Greater Elements
class Solution {
public int countElements(int[] nums) {
int mi = Arrays.stream(nums).min().getAsInt();
int mx = Arrays.stream(nums).max().getAsInt();
int ans = 0;
for (int x : nums) {
if (mi < x && x < mx) {
ans++;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2148: Count Elements With Strictly Smaller and Greater Elements
func countElements(nums []int) (ans int) {
mi := slices.Min(nums)
mx := slices.Max(nums)
for _, x := range nums {
if mi < x && x < mx {
ans++
}
}
return
}
# Accepted solution for LeetCode #2148: Count Elements With Strictly Smaller and Greater Elements
class Solution:
def countElements(self, nums: List[int]) -> int:
mi, mx = min(nums), max(nums)
return sum(mi < x < mx for x in nums)
// Accepted solution for LeetCode #2148: Count Elements With Strictly Smaller and Greater Elements
struct Solution;
impl Solution {
fn count_elements(nums: Vec<i32>) -> i32 {
let n = nums.len();
let min = *nums.iter().min().unwrap();
let max = *nums.iter().max().unwrap();
if min == max {
return 0;
}
let mut count = 0;
for i in 0..n {
if nums[i] == min || nums[i] == max {
count += 1;
}
}
(n - count) as i32
}
}
#[test]
fn test() {
let nums = vec![11, 7, 2, 15];
let res = 2;
assert_eq!(Solution::count_elements(nums), res);
let nums = vec![-3, 3, 3, 90];
let res = 2;
assert_eq!(Solution::count_elements(nums), res);
}
// Accepted solution for LeetCode #2148: Count Elements With Strictly Smaller and Greater Elements
function countElements(nums: number[]): number {
const mi = Math.min(...nums);
const mx = Math.max(...nums);
return nums.filter(x => mi < x && x < mx).length;
}
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.