Bob's Quest
Practice
3.7 (7 votes)
C++
Greedy algorithms
Algorithms
Observation
Sliding window
Basics of greedy algorithms
Problem
46% Success 1686 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Bob has an array A of size N, and he is very fond of two integers X and Y. Find the length of the longest subarray, such that it contains exactly X distinct integers and Y exist at least once in the subarray.

Input format

  • The first line contains an integer T, which denotes the number of test cases.
  • The first line of each test case contains three space separated integers N, X, Y denoting the size of array A, the value of X and Y, respectively.
  • The second line of each test case contains N space-separated integers, denoting the elements of array A.

Output format

For each test case, print the length of the largest subarray, such that it contains exactly X distinct integers and Y exist at least once in the subarray in a new line.

Constraints

1≤T≤10
1≤N≤105
0≤A[i]≤105
1≤X,Y≤105

Sample Input
2
3 2 1
2 3 1
1 1 2
1
Sample Output
2
0
Explanation

For test case 1: There exists only one subarray having exactly 2 distinct elements and having at least one occurrence of 1, i.e. [3,1] of length 2. Therefore, the answer will be 2.

For test case 2: 2 is not present in the whole array A. So, the answer is 0.

Code Editor

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Submissions
Please login to view your submissions
Similar Problems
Points:30
1 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsC++Greedy Algorithms
Points:30
20 votes
Tags:
ApprovedData StructuresGreedy AlgorithmsMediumOpenPriority queue
Points:30
5 votes
Tags:
Bit ManipulationAlgorithmsGreedy AlgorithmsBasics of Greedy Algorithms
Editorial

Login to unlock the editorial