Ctrl K

Segments With a Given LCM

Medium
You are given an array of positive integers values and a number multiple. Count the non-empty segments (contiguous pieces of the array) whose elements have a least common multiple of exactly multiple. The LCM of a set of numbers is the smallest positive integer divisible by each of them. Segments with different bounds count separately.
Example 1
values=[2, 3, 4, 6, 5], multiple=12 → 5

The LCM is 12 for [2, 3, 4], [3, 4], [3, 4, 6], [2, 3, 4, 6] and also [4, 6]. Any segment with 5 in it has an LCM divisible by 5.

Example 2
values=[5], multiple=10 → 0

The only segment, [5], has LCM 5, not 10.

Example 3
values=[1, 3, 1, 3], multiple=3 → 8
Constraints
  • 1 ≤ len(values) ≤ 1000
  • 1 ≤ values[i] ≤ 1000
  • 1 ≤ multiple ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPrimes Below a Bound MediumPhoto Frames of the Same Shape MediumSegments on a Chart MediumChunks With a Common Divisor MediumSegments With a Given GCD Medium
loading Python…
Input
values=[2, 3, 4, 6, 5], multiple=12
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 108 tests on the server, hidden ones included.