Count occurrences using binary search