The Number of Subarrays With LCM Equal to K problem asks you to count subarrays whose LCM (Least Common Multiple) is exactly k. Like GCD problems, LCM can only grow as the subarray expands, yielding at most O(log k) distinct LCM values per right endpoint. This coding problem uses the same compressed-values technique as GCD and AND subarray problems.
Unity and Amazon ask this because it requires recognizing that LCM grows monotonically, enabling O(n log k) total counting. Brute force O(n²) with LCM computation is too slow for large inputs. The array, math, and number theory interview pattern is demonstrated through the LCM compression technique.
Right-endpoint LCM compression. For each right endpoint j, maintain a list of (lcm_value, leftmost_start) pairs. Extend to j+1: compute lcm(existing_value, nums[j+1]) for each pair, merge entries with equal LCM (keeping leftmost start). For each pair with LCM=k, count subarrays from leftmost to the previous group's end. LCM can only increase and eventually exceed k, so the number of distinct LCMs stays O(log k).
nums=[3,2,6,1,4], k=6.
LCM and GCD subarray problems share the same "O(log max_val) distinct values per endpoint" property. For LCM: values can only grow as the window expands. For GCD: values can only shrink. Once LCM > k, it can never come back to k — prune immediately. Practice the GCD version first, then adapt for LCM by replacing gcd(a,b) with lcm(a,b) = a*b/gcd(a,b). These number theory subarray problems are favorites at competitive programming-heavy companies.