Count Subarrays Whose Least Common Multiple Equals K

Quick Overview

Count the contiguous subarrays of an integer array whose least common multiple equals a target k. It tests computing a running LCM with gcd, reasoning about how the LCM grows as a subarray extends, and pruning a quadratic enumeration early.

Count Subarrays Whose Least Common Multiple Equals K

Company: Unity

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `nums` and an integer `k`, return the number of subarrays of `nums` where the least common multiple of the subarray's elements is equal to `k`. A subarray is a contiguous non-empty sequence of elements within an array. The least common multiple of an array is the smallest positive integer that is divisible by all the array elements. ### Function Signature ```python def subarray_lcm(nums: list[int], k: int) -> int: ``` ### Rules - A subarray is `nums[i..j]` with `0 <= i <= j < len(nums)`. Subarrays with different start or end indices are counted separately, even when they hold the same values. - `k` does not have to appear in `nums`. Several smaller elements can combine to an LCM of `k`. ### Constraints - `1 <= len(nums) <= 1000` - `1 <= nums[i], k <= 1000` ### Examples ```text Input: nums = [3,6,2,7,1], k = 6 Output: 4 ``` The subarrays with LCM 6 are `[3,6]`, `[3,6,2]`, `[6]` and `[6,2]`. Any subarray that includes the 7 has an LCM that is a multiple of 7, and the remaining ones (`[3]`, `[2]`, `[1]`) have LCM 3, 2 and 1. ```text Input: nums = [3], k = 2 Output: 0 ``` The only subarray is `[3]`, whose LCM is 3, so no subarray has LCM 2. ```text Input: nums = [2,3,4,6], k = 12 Output: 5 ``` The subarrays with LCM 12 are `[3,4]`, `[4,6]`, `[2,3,4]`, `[3,4,6]` and `[2,3,4,6]`. The other five subarrays have LCM 2, 3, 4, 6 and 6 (`[2,3]`), so 12 is reached only by combining elements.

Overview: Count the contiguous subarrays of an integer array whose least common multiple equals a target k. It tests computing a running LCM with gcd, reasoning about how the LCM grows as a subarray extends, and pruning a quadratic enumeration early.

Unity logo
Unity
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums and an integer k, return the number of subarrays of nums where the least common multiple of the subarray's elements is equal to k.

A subarray is a contiguous non-empty sequence of elements within an array.

The least common multiple of an array is the smallest positive integer that is divisible by all the array elements.

Function Signature

def subarray_lcm(nums: list[int], k: int) -> int:

Rules

  • A subarray is nums[i..j] with 0 <= i <= j < len(nums) . Subarrays with different start or end indices are counted separately, even when they hold the same values.
  • k does not have to appear in nums . Several smaller elements can combine to an LCM of k .

Constraints

  • 1 <= len(nums) <= 1000
  • 1 <= nums[i], k <= 1000

Examples

Input: nums = [3,6,2,7,1], k = 6
Output: 4

The subarrays with LCM 6 are [3,6], [3,6,2], [6] and [6,2]. Any subarray that includes the 7 has an LCM that is a multiple of 7, and the remaining ones ([3], [2], [1]) have LCM 3, 2 and 1.

Input: nums = [3], k = 2
Output: 0

The only subarray is [3], whose LCM is 3, so no subarray has LCM 2.

Input: nums = [2,3,4,6], k = 12
Output: 5

The subarrays with LCM 12 are [3,4], [4,6], [2,3,4], [3,4,6] and [2,3,4,6]. The other five subarrays have LCM 2, 3, 4, 6 and 6 ([2,3]), so 12 is reached only by combining elements.