Ctrl K

Distinct Segments With Few Multiples

Medium
You are given an array values and two numbers, limit and divisor. Count the distinct non-empty contiguous segments of the array that contain at most limit elements divisible by divisor. Two segments are the same if they match element by element (same length, same numbers in the same places), even when they come from different parts of the array. An O(n²) solution exists.
Example 1
values=[1, 4, 1], limit=1, divisor=4 → 5

Only one number is divisible by 4, so every segment qualifies. Five of them are distinct: [1], [4], [1, 4], [4, 1] and [1, 4, 1]; the second [1] repeats the first.

Example 2
values=[5, 10, 5], limit=1, divisor=5 → 2

Every number is divisible by 5, so only single-element segments qualify: [5] and [10].

Constraints
  • 1 ≤ len(values) ≤ 2000
  • 1 ≤ values[i], divisor ≤ 200
  • 1 ≤ limit ≤ len(values)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTarget From Word Beginnings MediumPurge Twin Folders HardBookend Word Pairs EasyFraming Word Pairs HardSplit Into Dictionary Words Medium
loading Python…
Input
values=[1, 4, 1], limit=1, divisor=4
Expected
5

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