Subarrays with B Different Integers

Given an array of integers A and an integer B. Find and return the number of good subarrays of A.

A subarray of A is said to be good if the number of different integers in that subarray is exactly B.



Input Format

The first argument given is the integer array A.
The second argument given is an integer B.

Output Format

Return the number of good subarrays of A.

Constraints

1 <= length of the array <= 40000
1 <= A[i] <= length of the array
1 <= B <= length of the array

For Example

Input 1:
    A = [1, 2, 1, 2, 3]
    B = 2
Output 1:
    7
    Explanation 1:
     Subarrays having exacty 2 different elements are: 
        [1, 2], [2, 1], [1, 2], [2, 3], [1, 2, 1], [2, 1, 2], [1, 2, 1, 2].

Input 2:
    A = [1, 2, 1, 3, 4]
    B = 3
Output 2:
    3
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Subarrays with B Different Integers on Interview Code Editor
Sign Up
to access hints and editorial solutions for Subarrays with B Different Integers

Discussion


Loading...
Click here to start solving coding interview questions