EasyNo. 2148
Count Elements With Strictly Smaller and Greater Elements — LeetCode 2148 Python Solution
- Pattern
- Sorting
- Topics
- Array, Counting, Sorting
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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
- 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.
Python solution
class Solution:
def countElements(self, nums: List[int]) -> int:
mi, mx = min(nums), max(nums)
return sum(mi < x < mx for x in nums)Complexity
- Time
- O(n)where n is the length of the array nums
- Space · auxiliary
- O(1)
Pattern: Sorting
Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 2148. Count Elements With Strictly Smaller and Greater Elements is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.
The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0056 | Merge Intervals | Medium |
| 0164 | Maximum Gap | Medium |
| 0274 | H-Index | Medium |
| 0406 | Queue Reconstruction by Height | Medium |
| 0414 | Third Maximum Number | Easy |
| 0462 | Minimum Moves to Equal Array Elements II | Medium |
Frequently asked questions
- How hard is LeetCode 2148. Count Elements With Strictly Smaller and Greater Elements?
LeetCode 2148. Count Elements With Strictly Smaller and Greater Elements is rated Easy on LeetCode.
- What is the time complexity of LeetCode 2148. Count Elements With Strictly Smaller and Greater Elements?
The Python solution on this page runs in O(n), where n is the length of the array \textit{nums}.
- What is the space complexity of LeetCode 2148. Count Elements With Strictly Smaller and Greater Elements?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2148. Count Elements With Strictly Smaller and Greater Elements cover?
LeetCode 2148. Count Elements With Strictly Smaller and Greater Elements is tagged Array, Counting and Sorting on LeetCode.