CCC '12 S3 - Absolutely Acidic

View as PDF

Submit solution

Points: 7
Time limit: 3.0s
Memory limit: 256M

Problem type
Canadian Computing Competition: 2012 Stage 1, Senior #3

You are gathering readings of acidity level in a very long river in order to determine the health of the river. You have placed N sensors (2 \le N \le 2\,000\,000) in the river, and each sensor gives an integer reading R (1 \le R \le 1\,000). For the purposes of your research, you would like to know the frequency of each reading, and find the absolute difference between the two most frequent readings.

If there are more than two readings that have the highest frequency, the difference computed should be the largest such absolute difference between two readings with this frequency. If there is only one reading with the largest frequency, but more than one reading with the second largest frequency, the difference computed should be the largest absolute difference between the most frequently occurring reading and any of the readings which occur with second-highest frequency.

Input Specification

The first line of input will be the integer N (2 \le N \le 2\,000\,000), the number of sensors. The next N lines each contain the reading for that sensor, which is an integer R (1 \le R \le 1\,000). You should assume that there are at least two different readings in the input.

Output Specification

Output the positive integer value representing the absolute difference between the two most frequently occurring readings, subject to the tie-breaking rules outlined above.

Sample Input 1

5
1
1
1
4
3

Output for Sample Input 1

3

Sample Input 2

4
10
6
1
8

Output for Sample Input 2

9

Comments


  • 1
    jerrycui07  commented on Jan. 11, 2023, 5:26 a.m.

    My first try I got AC on test case 3-5 but WA on 1-2 . . . my second try I got AC 1-2, 4-5, but WA 3 . . .


  • 3
    Cameo5958  commented on Oct. 2, 2021, 8:28 p.m. edited

    Quick question, when it says "absolute" does it mean "the absolute biggest value it can be"?

    EDIT: Sorry, I'm an idiot. I didn't read the other bit.


    • 8
      ayay9  commented on Oct. 2, 2021, 8:43 p.m.

      No, it means the absolute difference


      • 4
        Yash_uppal  commented on Feb. 11, 2022, 6:36 p.m.

        Absolute difference like [x - y]?


  • 3
    AryanG  commented on Feb. 12, 2020, 3:36 p.m.

    What should be the output if there is only one highest frequency? Eg: 5 1 1 1 1 1


    • 12
      d  commented on Dec. 17, 2020, 6:59 p.m. edit 2

      If you're asking about 5 readings that are all 1's, the problem statement says

      You should assume that there are at least two different readings in the input.

      If you're asking about 6 readings (a 5 and multiple 1's), the problem statement's first paragraph has already defined the correct output.