Added code for counting maximum number of gaps.