Implemented the pair counting algorithm.