Comments

No, after sorting, each group contains lines of the same gradient and the same intercept (or in another word, same line). So, in a arbitrary group, suppose you have K lines with the same gradient/intercept in it, then K must satisfy K = P * (P — 1) / 2, with P is the number of flamingo in that group (it means all those P flamingoes lie in the same line, right?). You can easily find that number P for each group. If the line representing that group passes through a binocular, assign that number P to that binocular, or keep the larger number if that binocular has been assigned already.

now I see, thanks Egor