Problem Description
Given an integer array, return the number of elements that have both a strictly smaller and a strictly greater element present in the array.
Key Insights
- The smallest and largest elements of the array cannot have both a strictly smaller and a strictly greater element.
- If the smallest and largest values are equal, no element qualifies.
- The solution focuses on counting elements that lie strictly between the minimum and maximum values.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the array, since we traverse the array a few times. Space Complexity: O(1), as we only use a fixed amount of extra space.
Solution
The solution involves the following steps:
- Identify the minimum and maximum values in the array.
- Traverse the array and count elements that are strictly greater than the minimum and strictly less than the maximum.
- Return the count.
We use simple comparisons and count variables to solve the problem in a single pass (after determining the min and max). This approach leverages constant space and linear time.