MODTEMP - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

The temperature on N days was measured, and it was A_i on the i-th day.
On how many days was the temperature not an extreme, i.e. neither minimum nor maximum?

EXPLANATION:

Let mn denote the minimum temperature reached across all N days.
mn equals \min(A), and can be found in \mathcal{O}(N) time by iterating through the array.

Similarly, let mx denote the maximum temperature reached across all N days.
This can also be found in \mathcal{O}(N) time.

Then, the temperature on day i is not extreme if and only if A_i \ne mn and A_i \ne mx.
Once mn and mx are known, this can be found in \mathcal{O}(N) as well by once again, iterating through the array.

TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

CODE:

Editorialist's code (PyPy3)
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    
    mn = min(a)
    mx = max(a)
    ans = 0
    for i in range(n):
        if a[i] != mn and a[i] != mx: ans += 1
    print(ans)