UberOA #26
Count Elements with at Least One Smaller and One Greater Value
EasyJudge-ready
Topics (AI-labelled)In-place Arrays
Given an integer array nums, count how many array elements have both a strictly smaller element and a strictly greater element. This is equivalent to counting the positions that satisfy min(nums) < nums[i] < max(nums); when the same value appears at several positions, every qualifying position is counted.
This site's valid input ranges: 1≤n≤100000, −10^9≤nums[i]≤10^9.
Input
The first line contains n; the second line contains n integers nums[i].
Output
Output the number of array positions that satisfy min(nums)<nums[i]<max(nums).
Example 1
Input:
3
1 2 3
Output:
1
Example 2
Input:
1
7
Output:
0
Example 3
Input:
2
4 4
Output:
0
Example 4
Input:
4
1 2 2 3
Output:
2
Example 5
Input:
5
-5 -1 -1 0 8
Output:
3
Example 6
Input:
3
-1000000000 0 1000000000
Output:
1
Example 7
Input:
100
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000
Output:
20
Example 8
Input:
4
-9 -8 -7 -6
Output:
2
Example notes
As long as a value is strictly greater than the global minimum and strictly less than the global maximum, it has both a smaller and a greater element. Count all qualifying positions without deduplicating.
Editorial
Loading solution…