Problem

Given an integer array nums, return _the number of elements that haveboth a strictly smaller and a strictly greater element appear in _nums.

Examples

Example 1

1
2
3
4
5
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

1
2
3
4
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
  • -10^5 <= nums[i] <= 10^5

Solution

Method 1 – Min/Max Filtering

Intuition

Only elements that are strictly greater than the minimum and strictly less than the maximum value in the array can have both a strictly smaller and a strictly greater element.

Approach

  1. Find the minimum and maximum values in the array.
  2. Count the number of elements that are strictly greater than the minimum and strictly less than the maximum.
  3. Return this count.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
public:
    int countElements(vector<int>& nums) {
        int mn = *min_element(nums.begin(), nums.end());
        int mx = *max_element(nums.begin(), nums.end());
        int ans = 0;
        for (int x : nums) if (x > mn && x < mx) ans++;
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func countElements(nums []int) int {
    mn, mx := nums[0], nums[0]
    for _, x := range nums {
        if x < mn { mn = x }
        if x > mx { mx = x }
    }
    ans := 0
    for _, x := range nums {
        if x > mn && x < mx { ans++ }
    }
    return ans
}
1
2
3
4
5
6
7
8
9
class Solution {
    public int countElements(int[] nums) {
        int mn = Arrays.stream(nums).min().getAsInt();
        int mx = Arrays.stream(nums).max().getAsInt();
        int ans = 0;
        for (int x : nums) if (x > mn && x < mx) ans++;
        return ans;
    }
}
1
2
3
4
5
6
7
class Solution {
    fun countElements(nums: IntArray): Int {
        val mn = nums.minOrNull()!!
        val mx = nums.maxOrNull()!!
        return nums.count { it > mn && it < mx }
    }
}
1
2
3
4
class Solution:
    def countElements(self, nums: list[int]) -> int:
        mn, mx = min(nums), max(nums)
        return sum(1 for x in nums if mn < x < mx)
1
2
3
4
5
6
7
impl Solution {
    pub fn count_elements(nums: Vec<i32>) -> i32 {
        let mn = *nums.iter().min().unwrap();
        let mx = *nums.iter().max().unwrap();
        nums.iter().filter(|&&x| x > mn && x < mx).count() as i32
    }
}
1
2
3
4
5
6
class Solution {
    countElements(nums: number[]): number {
        const mn = Math.min(...nums), mx = Math.max(...nums);
        return nums.filter(x => x > mn && x < mx).length;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of the array, for finding min, max, and counting.
  • 🧺 Space complexity: O(1), only a constant amount of space is used.