CCC '12 S3 - Absolutely Acidic

View as PDF

Submit solution

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

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
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


Output for Sample Input 1


Sample Input 2


Output for Sample Input 2



  • 0
    balathegreat999  commented on May 27, 2020, 1:27 p.m. edited

    nvm I figured it out

    • 0
      randomperson2  commented on June 5, 2020, 3:32 p.m.

      5 = N but that first number is just telling you how many sensor readings there are (it's not a reading value). You're computing the absolute difference between the readings, R which are on every line after the first.

  • -1
    AryanG  commented on Feb. 12, 2020, 10:36 a.m.

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

    • -2
      Evan  commented on Feb. 13, 2020, 2:44 p.m. edit 2

      It should be 5 - 1 = 4.

      The question says to pick the one with the highest frequency, and then pick the next highest occurring frequency. If there are ties, output the one with the highest difference.

  • -4
    Xenosi  commented on March 18, 2019, 11:03 a.m. edited

    Learning more about how much your input affects your code everyday...