
You are given, ‘arr’ = [1, 2, 1, 1, 1, 1] and ‘favArr’ = [1, 1, 2], then all the subarrays which contains the same elements as in the ‘favArr’ are [1, 2, 1] , [1, 2, 1, 1], [1, 2, 1, 1, 1], [1, 2, 1, 1, 1, 1], [2, 1, 1], [2, 1, 1, 1], [2, 1, 1, 1, 1]. There are total of 7 sub arrays. Hence the answer is 7.
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘K’, representing the size of ‘arr’ and ‘favArr’, respectively.
The second line of each test case contains ‘N’ space-separated integers representing the elements of the array ‘arr.’
The second line of each test case contains ‘K’ space-separated integers representing the elements of the array ‘favArr.’
For each test case, print a single integer representing the number of subarrays containing the integers in the ‘favArr’ at least once.
Print a separate line of each test case.
1 <= T <= 10
1 <= N <= 10^6
1 <= K <= N
0 <= arr[i], favArr[i] <= 10^9
Time Limit: 1 sec
You do not need to print anything. It has already been taken care of. Just implement the function.
This approach will iterate over all the instances of all arrays and count the number of subarrays that satisfy the condition given.
We will create a checkSubArr(subArr, favDict), where subArr is the subarray that will be checked and facDict is the frequency count of the elements in the favArr
Algorithm:
In this approach, we will store the number of occurrences of each integer in the favArr.
We will create two pointers start and end, and store each favourite integer’s occurrences in a map.
Then when we iterate end through the arr, and if we find the element in the map, we decrease its count, and whenever an integer has a count of 0 in the map, that means it is included in our current subarray sufficient times. When all the integers in the map are 0, we add all the subarrays to the answer.
Algorithm: