---
{"category_name":"easy","problem_code":"MEXPROB","problem_name":"Yet another MEX problem","problemComponents":{"constraints":"- $1 \\leq T \\leq 3 \\cdot 10^4$\n- $1 \\leq N \\leq 10^5$\n- $1 \\leq K \\leq \\frac{N \\cdot (N + 1)}{2}\\ $\n- $0 \\leq A_i \\leq N$\n- Sum of $N$ over all test cases does not exceed $2\\cdot10^6$.\n","constraintsState":true,"subtasks":"**Subtask 1 (10 points):**\n- $1 \\leq N \\leq 5 \\cdot 10^3$\n- Sum of $N$ over all test cases does not exceed $5 \\cdot 10^4$.\n\n\n**Subtask 2 (90 points):** Original constraints","subtasksState":true,"inputFormat":"- The first line contains $T$ denoting the number of test cases. Then the test cases follow.\n- The first line of each test case contains two space-separated integers $N$ and $K$.\n- The second line contains $N$ space-separated integers $A_1, A_2, \\dots, A_N$ denoting the given array.","inputFormatState":true,"outputFormat":"For each test case, output on a single line the $K$-th smallest value in the list $B$.","outputFormatState":true,"sampleTestCases":{"0":{"id":1,"input":"3\n3 4\n1 0 2\n3 2\n2 1 3\n3 6\n0 1 2","output":"1\n0\n3\n","explanation":"**Test case $1$:** $MEX(A_1) = 0$, $MEX(A_1, A_2) = 2$, $MEX(A_1, A_2, A_3) = 3$, $MEX(A_2) = 1$, $MEX(A_2, A_3) = 1$, $MEX(A_3) = 0$. Hence the list $B = [0, 2, 3, 1, 1, 0]$ and the $4$-th smallest value in $B$ is $1$.\n\n**Test case $2$:** The MEX of all subarrays of the array $A$ is $0$. Hence the $2$-nd smallest element in the list $B$ is $0$.","isDeleted":false}}},"video_editorial_url":"https://youtu.be/aFodOFNNSOA","languages_supported":{"0":"CPP14","1":"C","2":"JAVA","3":"PYTH 3.6","4":"CPP17","5":"PYTH","6":"PYP3","7":"CS2","8":"ADA","9":"PYPY","10":"TEXT","11":"PAS fpc","12":"NODEJS","13":"RUBY","14":"PHP","15":"GO","16":"HASK","17":"TCL","18":"PERL","19":"SCALA","20":"LUA","21":"kotlin","22":"BASH","23":"JS","24":"LISP sbcl","25":"rust","26":"PAS gpc","27":"BF","28":"CLOJ","29":"R","30":"D","31":"CAML","32":"FORT","33":"ASM","34":"swift","35":"FS","36":"WSPC","37":"LISP clisp","38":"SQL","39":"SCM guile","40":"PERL6","41":"ERL","42":"CLPS","43":"ICK","44":"NICE","45":"PRLG","46":"ICON","47":"COB","48":"SCM chicken","49":"PIKE","50":"SCM qobi","51":"ST","52":"SQLQ","53":"NEM"},"max_timelimit":1,"source_sizelimit":50000,"problem_author":"soumyadeep_21","problem_tester":"","date_added":"21-09-2021","tags":{"0":"binary","1":"easy","2":"oct21","3":"soumyadeep_21","4":"two"},"problem_difficulty_level":"Unavailable","best_tag":"Binary Search","editorial_url":"https://discuss.codechef.com/problems/MEXPROB","time":{"view_start_date":1633944600,"submit_start_date":1633944600,"visible_start_date":1633944600,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=MEXPROB","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
### Read problem statements in [Bengali](https://www.codechef.com/download/translated/OCT21/bengali/MEXPROB.pdf), [Mandarin Chinese](https://www.codechef.com/download/translated/OCT21/mandarin/MEXPROB.pdf), [Russian](https://www.codechef.com/download/translated/OCT21/russian/MEXPROB.pdf), and [Vietnamese](https://www.codechef.com/download/translated/OCT21/vietnamese/MEXPROB.pdf) as well.
The *MEX* (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:
- The MEX of $[2, 2, 1]$ is $0$, because $0$ does not belong to the array.
- The MEX of $[3, 1, 0, 1]$ is $2$, because $0$ and $1$ belong to the array, but $2$ does not.
- The MEX of $[0, 3, 1, 2]$ is $4$, because $0, 1, 2$ and $3$ belong to the array, but $4$ does not.
You are given an array $A$ of length $N$. You create a list $B$ consisting of the MEX-es of all subarrays of the array $A$. Formally, for all pairs $(l, r)$ such that $1 \leq l \leq r \leq N$, you calculate $MEX(A_l, A_{l + 1}, \dots, A_r)$ and append the value in the list $B$. Find the $K$-th smallest value in the list $B$.
**Note:** Since the size of the input and output is large, please use fast input-output methods.
<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>