Skip to content

2148. Count Elements With Strictly Smaller and Greater Elements 👍

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
9
class Solution {
 public:
  int countElements(vector<int>& nums) {
    const int min = ranges::min(nums);
    const int max = ranges::max(nums);
    return ranges::count_if(
        nums, [&](const int num) { return min < num && num < max; });
  }
};
1
2
3
4
5
6
7
class Solution {
  public int countElements(int[] nums) {
    final int min = Arrays.stream(nums).min().getAsInt();
    final int max = Arrays.stream(nums).max().getAsInt();
    return (int) Arrays.stream(nums).filter(num -> min < num && num < max).count();
  }
}
1
2
3
4
5
class Solution:
  def countElements(self, nums: List[int]) -> int:
    mini = min(nums)
    maxi = max(nums)
    return sum(mini < num < maxi for num in nums)