All problems

Detecting Outliers With the IQR Rule

hardPythonFeature Engineering

Quillon Energy's batches occasionally carry a reading that is plainly wrong -- a decimal point in the wrong place, a meter read in the wrong unit -- and the team wants them counted before the batch goes near a model. Their rule works from quartiles, and Quillon pins down exactly which ones. Put the readings in order and split them into a bottom half and a top half around the middle; when the count is odd, the one middle reading belongs to neither half. Each half's quartile is that half's own middle value, or the average of its two middle values when the half has an even count. The gap between the two quartiles sets the fence: a reading more than one and a half of those gaps below the bottom quartile, or more than that above the top one, is flagged. A reading landing exactly on a fence is not flagged. Readings arrive in no particular order.

Task: Print how many readings the rule flags.

Input

The first line holds one integer n, the number of readings. The second line holds the n readings separated by single spaces.

Output

One line holding the count of flagged readings, from both ends of the batch together. A batch whose readings are all the same has no spread at all, and nothing in it counts as unusual — report 0 rather than flagging every reading.

Example:

Input:
12
1 2 3 4 5 6 7 8 9 10 11 100

Output:
1

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…