TopCoder problem "Harmony" used in SRM 176 (Division II Level Three)



Problem Statement

    A chord in music is formed by playing three or more notes at the same time. The sound of the chord depends of the ratio of each note in the chord to the others. The ratios between frequencies in harmonious chords can be expressed as rational numbers with small denominators (2/1, 3/2, 5/4, etc...), whereas the ratios between frequencies in discordant chords can only be expressed as rational numbers with large denominators (8/7, 16/9, etc...). Ratios between frequencies are always represented by reduced fractions with the smaller number in the denominator. Create a class Harmony with a function mostHarmonious that takes a int[] frequencies representing the frequencies of various notes and returns a int[] that contains the three elements from frequencies that form the most harmonious chord, sorted from lowest to highest frequency.



A chord C1 is more harmonious than a chord C2 if the largest denominator of the ratios in C1 is smaller than the largest denominator of the ratios in C2. If these values are the same, then compare the second largest denominator of each chord in the same way, and then the third largest if necessary. If two chords have the same denominators, compare the lowest frequency of each chord, and return the chord with the lower frequency. If they are the same, then compare the second lowest frequency of each chord in the same way, and then the third lowest, if necessary.



An example: a chord C1 has notes with frequencies 200, 250, and 400. The ratios of these notes are 5/4, 2/1, and 8/5. A chord C2 has notes with frequencies 200, 320, and 350. The ratios of these notes are 8/5, 7/4, and 5/4. The denominators of the ratios in C1 are 1, 4, and 5, and for C2 they are 4, 4, and 5. C1 is the more harmonious of the two, because although both chords have the same largest and second largest denominator, C1's third largest denominator is less than C2's third largset denominator.
 

Definition

    
Class:Harmony
Method:mostHarmonious
Parameters:int[]
Returns:int[]
Method signature:int[] mostHarmonious(int[] frequencies)
(be sure your method is public)
    
 

Constraints

-frequencies will contain between 3 and 50 elements, inclusive.
-Every element of frequencies will be a value between 1 and 10000, inclusive.
 

Examples

0)
    
{200,250,400,320,350}
Returns: { 200,  250,  400 }
These are the frequencies used in the example above. The most harmonious chord has ratios 8/5 (400/250), 5/4 (250/200), and 2/1 (400/200).
1)
    
{440, 320, 750,660, 500,550}
Returns: { 440,  550,  660 }
The best chord here is an A major triad.
2)
    
{1960,1000,3050,2341,7253,7864,2000,2352,2940,1534,7234}
Returns: { 1960,  2352,  2940 }
The best chord here is a G minor triad. Notice that even though the frequencies 1000 and 2000 are available, which had a ratio of 2/1, there is nothing else they could be used with that would also have a low ratio.
3)
    
{100,200,300,400,500,600,700,800,900,1000}
Returns: { 100,  200,  400 }

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=2015

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4685&pm=2015

Writer:

Running Wild

Testers:

lbackstrom , brett1479

Problem categories:

Brute Force, Math