The Number of Subarrays With GCD Equal to K problem asks you to count subarrays whose GCD is exactly k. This coding problem exploits the fact that as a subarray grows, the GCD can only decrease or stay the same — yielding O(log max_val) distinct GCD values per right endpoint.
Amazon asks this because it demonstrates the "at most O(log max_val) distinct GCDs" property, enabling efficient O(n log n) counting instead of O(n²). The array, math, and number theory interview pattern is directly applied with GCD monotonicity as the key insight.
Right endpoint scan with GCD compression. For each right endpoint j, maintain a list of (gcd_value, leftmost_start) pairs for subarrays ending at j. Extend to j+1 by computing gcd of each pair's value with nums[j+1]. Merge entries with equal GCD values (keeping leftmost start). For each pair with GCD=k, count the subarrays (rightmost_start - leftmost_start + 1).
nums=[9,3,1,2,6,3], k=3.
GCD subarray problems mirror AND subarray problems in their O(log max_val) distinct-values property. The pattern is identical: maintain (value, start) pairs, merge on equal values when extending. For GCD-equals-k counting, this gives O(n log n) overall. Practice this "compressed values" technique for GCD, LCM, AND, OR — they all share the same monotone decrease/increase property that enables efficient subarray enumeration. DE Shaw and Amazon frequently test these advanced bit/number theory subarray problems.