including the code to pick up pattern of notes
Algorithm updated