Back to course home
0% completed
Vote For New Content
Count the Number of Good Subarrays (medium)
Problem Statement
Given an array of integers nums
, and an integer k
, find the count of "good" subarrays within nums
.
A subarray is considered "good" if it contains at least k
pairs of elements (i, j)
where i < j
and nums[i] == nums[j]
.
A subarray is a contiguous sequence
of elements in the original array.
Examples
-
Example 1:
- Input:
nums = [2, 2, 2, 3, 3], k = 3
- Expected Output:
3
- Justification: There are 3 good subarrays that meet the criteria:
[2, 2, 2, 3]
,[2, 2, 2, 3, 3]
, and[2, 2, 2]
. Each of these subarrays has at least 3 pairs of equal elements.
- Input:
-
Example 2:
- Input:
nums = [4, 4, 4, 4], k = 6
- Expected Output:
1
- Justification: The only good subarray that meets the criteria is the entire array itself
[4, 4, 4, 4]
, which contains 6 pairs of equal elements.
- Input:
-
Example 3:
- Input:
nums = [1, 2, 3, 1, 2], k = 2
- Expected Output:
2
- Justification: There is only 1 good subarray, which is
[1, 2, 3, 1, 2]
. It contains at least 2 pairs of equal elements.
- Input:
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible