2148. Count Elements With Strictly Smaller and Greater Elements LeetCode Solution
In this guide, you will get 2148. Count Elements With Strictly Smaller and Greater Elements LeetCode Solution with the best time and space complexity. The solution to Count Elements With Strictly Smaller and Greater Elements problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Elements With Strictly Smaller and Greater Elements solution in C++
- Count Elements With Strictly Smaller and Greater Elements solution in Java
- Count Elements With Strictly Smaller and Greater Elements solution in Python
- Additional Resources
Problem Statement of Count Elements With Strictly Smaller and Greater Elements
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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2148. Count Elements With Strictly Smaller and Greater Elements LeetCode Solution in C++
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; });
}
};
/* code provided by PROGIEZ */
2148. Count Elements With Strictly Smaller and Greater Elements LeetCode Solution in Java
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();
}
}
// code provided by PROGIEZ
2148. Count Elements With Strictly Smaller and Greater Elements LeetCode Solution in Python
class Solution:
def countElements(self, nums: list[int]) -> int:
mn = min(nums)
mx = max(nums)
return sum(mn < num < mx for num in nums)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.