Minimum Length

2.5

61 votes
Arrays, 1-D, Data Structures
Problem

You are given two arrays A and B, of length N. You can select any subarray and then sort the elements in ascending order of that subarray for arrays A and B.

Find the minimum length of the subarray you can choose to make A and B same after performing the operation. A and B are permutations of each other.

Input Format:

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains an integer N.
  • The next line of each test case contains N space-separated integers, elements of array A.
  • The next line of each test case contains N space-separated integers, elements of array B.

Output Format:

For each test case, print the minimum length of the subarray you can choose to make A and B same after performing the operation.

Constraints:

1T101N1051A[i],B[i]105

 

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

First test case:
We can choose a subarray from index 2 to 3 (1-based indexing). Hence, the answer is 2.

Second test case:
We can choose a subarray from index 1 to 3 (1-based indexing). Hence, the answer is 3.

Editor Image

?