Ctrl K

Segments With Enough Matches

Medium
You are given an integer array values and an integer need. Within a segment of the array, a *match* is a pair of positions i < j inside it with values[i] == values[j]. For instance, [5, 5, 5] has three matches. Count the contiguous non-empty segments of values that contain at least need matches.
Example 1
values=[2, 7, 2, 7], need=2 → 1

Only the whole array has two matching pairs: the 2s at 0 and 2, and the 7s at 1 and 3.

Example 2
values=[4, 4, 4], need=1 → 3

[4, 4] qualifies twice (at two positions), and so does [4, 4, 4] with three pairs.

Example 3
values=[1, 2, 3], need=1 → 0
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁹
  • 1 ≤ need ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo-Genre Stretch of a Playlist MediumWindows With an Exact Alarm Count MediumStretches With k Odd Numbers MediumTwo Separate Segments With a Given Sum MediumTrim the Ends to a Target Medium
loading Python…
Input
values=[2, 7, 2, 7], need=2
Expected
1

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