Install our extension to search inside any video instantly.

Leetcode Daily . Sorted GCD Pair Queries

Added:
137 views0likes37:36ransack10Original Release: 2026-07-17

To efficiently count pairs with a specific GCD value in an array, use the inclusion-exclusion principle: count all pairs where both elements are divisible by g, then subtract pairs where both elements are divisible by multiples of g (2g, 3g, etc.). This approach reduces complexity from O(n²) to O(m log m) where m is the maximum array value, making it feasible for large inputs (n up to 10^5).