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 mn = ranges::min(nums);
    const int mx = ranges::max(nums);
    return ranges::count_if(
        nums, [&](const int num) { return mn < num && num < mx; });
  }
};
1
2
3
4
5
6
7
class Solution {
  public int countElements(int[] nums) {
    final int mn = Arrays.stream(nums).min().getAsInt();
    final int mx = Arrays.stream(nums).max().getAsInt();
    return (int) Arrays.stream(nums).filter(num -> mn < num && num < mx).count();
  }
}
1
2
3
4
5
class Solution:
  def countElements(self, nums: List[int]) -> int:
    mn = min(nums)
    mx = max(nums)
    return sum(mn < num < mx for num in nums)