Count Elements With Strictly Smaller and Greater Elements

easy array

Problem

Given an integer array nums, return the number of elements that have both a strictly smaller and a strictly greater element appearing in nums.

Inputnums = [11,7,2,15]
Output2
The min is 2 and the max is 15. Only 11 and 7 sit strictly between them.

def count_elements(nums):
    lo, hi = min(nums), max(nums)
    count = 0
    for x in nums:
        if lo < x < hi:
            count += 1
    return count
function countElements(nums) {
  const lo = Math.min(...nums);
  const hi = Math.max(...nums);
  let count = 0;
  for (const x of nums) {
    if (x > lo && x < hi) count++;
  }
  return count;
}
class Solution {
    public int countElements(int[] nums) {
        int lo = Integer.MAX_VALUE, hi = Integer.MIN_VALUE;
        for (int x : nums) { lo = Math.min(lo, x); hi = Math.max(hi, x); }
        int count = 0;
        for (int x : nums) if (x > lo && x < hi) count++;
        return count;
    }
}
int countElements(vector<int>& nums) {
    int lo = *min_element(nums.begin(), nums.end());
    int hi = *max_element(nums.begin(), nums.end());
    int count = 0;
    for (int x : nums) if (x > lo && x < hi) count++;
    return count;
}
Time: O(n) Space: O(1)