Broken Piano
InterviewTime limit0.5sMemory limit1024 MB
Given broken piano keys and a MIDI piece, count the notes that cannot be played and find the smallest transposition that makes the whole piece playable.
- Level
Easy3 of 10
- Topics
- Brute force, Array, Implementation
- Solved
- No attempts yet
Problem
MIDI (Musical Instrument Digital Interface) is a technical standard for describing musical pieces. Each note is an integer in the range . You have a piano with keys and want to play a piece given in MIDI format. The leftmost key is MIDI code , the second key from the left is code , and so on.
Unfortunately the piano is old, and some keys are broken. You know which keys work and which do not. No working key breaks while the piece is played, and no broken key starts working.
Write a program that finds:
- the number of notes in the piece that cannot be played on the piano, and
- the minimal transposition needed to play the piece on the piano.
Transposition shifts every note in the piece up or down by the same amount. When the piece is transposed up by one half-tone, each note is played on the key to its right instead. When it is transposed down by one half-tone, the key to its left is played. Two half-tones up means the key two positions to the right, and so on. When transposed up by one half-tone, the note with MIDI code cannot be played, because no key corresponds to it.
Input
The first line contains , the number of broken keys (). The second line contains distinct integers (), the MIDI codes of the broken keys. The third line contains , the number of notes in the piece (). The fourth line contains integers (), the MIDI codes of the notes.
Output
Output two lines. The first line is the number of notes in the piece that cannot be played on the piano. The second line is the number of half-tones the piece must be transposed by so that it can be played. If several transpositions work, output the one with the smallest absolute value. If no transposition works, output X on the second line. If your solution cannot determine the transposition, output E on the second line.