New update is available. Click here to update.

Last Updated: 8 Mar, 2021

Difficulty: Moderate

```
All the elements in the ‘SMALL’ array are distinct.
```

```
Let us say 'SMALL' = [ 3,6 ] and 'LARGE' = [ 8, 6, 9, 3, 1, 2, 6].
Subarray [ 1,3 ] (from index 1 to index 3) and [ 3,6 ] have all the elements that are present
In ‘SMALL’ array. The length of the sub-array [ 1, 3 ] is shorter. Therefore required answer = 3
```

```
The first line contains a single integer ‘T’ denoting the number of test cases.
The first line of each test contains two integers ‘M’ and ‘N’ - the number of elements in the ‘LARGE’ and ‘SMALL’ array respectively.
The third line of each test case contains ‘M’ space-separated integers that make up ‘LARGE’.
The fourth line of each test case contains ‘N’ space-separated integers that makeup ‘SMALL’.
```

```
For each test case, print an integer denoting the length of the shortest sub-array of ‘LARGE’ having all elements of ‘SMALL’.
If no such subarray exists, return ‘-1’.
```

```
You are not required to print anything; it has already been taken care of. Just implement the function and return the matrix.
```

```
1 <= T <= 50
1 <= M,N <= 10^4
1 <= LARGE[ i ] , SMALL[ i ] <=10^8
Time Limit: 1 sec
```

SIMILAR PROBLEMS

Longest Subarray With Zero Sum

Posted: 3 Nov, 2022

Difficulty: Moderate

Merge Two Sorted Arrays Without Extra Space

Posted: 19 Nov, 2022

Difficulty: Moderate

Ninja And The Strictly Increasing Array

Posted: 27 Nov, 2022

Difficulty: Moderate

Maximum GCD

Posted: 8 Dec, 2022

Difficulty: Hard

Negative To The End

Posted: 16 Dec, 2022

Difficulty: Easy

Popular Interview Problems: