Description#
Given an integer array nums
, return the number of elements that have both a strictly smaller and a strictly greater element appear in nums
.
Example 1:
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:
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
-105 <= nums[i] <= 105
Solutions#
Solution 1#
1
2
3
4
| class Solution:
def countElements(self, nums: List[int]) -> int:
mi, mx = min(nums), max(nums)
return sum(mi < num < mx for num in nums)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| class Solution {
public int countElements(int[] nums) {
int mi = 1000000, mx = -1000000;
for (int num : nums) {
mi = Math.min(mi, num);
mx = Math.max(mx, num);
}
int ans = 0;
for (int num : nums) {
if (mi < num && num < mx) {
++ans;
}
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| class Solution {
public:
int countElements(vector<int>& nums) {
int mi = 1e6, mx = -1e6;
for (int num : nums) {
mi = min(mi, num);
mx = max(mx, num);
}
int ans = 0;
for (int num : nums)
if (mi < num && num < mx)
++ans;
return ans;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| func countElements(nums []int) int {
mi, mx := int(1e6), -int(1e6)
for _, num := range nums {
if num < mi {
mi = num
}
if num > mx {
mx = num
}
}
ans := 0
for _, num := range nums {
if mi < num && num < mx {
ans++
}
}
return ans
}
|
1
2
3
4
5
6
7
8
9
10
11
12
| function countElements(nums: number[]): number {
const min = Math.min(...nums),
max = Math.max(...nums);
let ans = 0;
for (let i = 0; i < nums.length; ++i) {
let cur = nums[i];
if (cur < max && cur > min) {
++ans;
}
}
return ans;
}
|