Input: nums =[11,7,2,15]Output: 2Explanation: 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.
Input: nums =[-3,3,3,90]Output: 2Explanation: 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.
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.
classSolution {
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;
}
};
classSolution {
publicintcountElements(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
classSolution {
funcountElements(nums: IntArray): Int {
val mn = nums.minOrNull()!!val mx = nums.maxOrNull()!!return nums.count { it > mn &&it < mx }
}
}
1
2
3
4
classSolution:
defcountElements(self, nums: list[int]) -> int:
mn, mx = min(nums), max(nums)
return sum(1for x in nums if mn < x < mx)
1
2
3
4
5
6
7
impl Solution {
pubfncount_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() asi32 }
}