You have been given two arrays having an equal number of elements. You have to find whether one array is the valid stack permutation of the other. An array is said to be a valid stack permutation of the other if and only if after applying some push and pop operations onto the sequence of elements in that array, will result in the other array.
Example:
Consider array : 2 4 6.
Valid stack permutations are as follows:
2 4 6
push ‘2’
pop ‘2’
push ‘4’
pop ‘4’
push ‘6’
pop ‘6’
2 6 4
push ‘2’
pop ‘2’
push ‘4’
pop ‘6’
push ‘6’
pop ‘4’
4 2 6
push ‘2’
pop ‘4’
push ‘4’
pop ‘2’
push ‘6’
pop ‘6’
4 6 2
push ‘2’
pop ‘4’
push ‘4’
pop ‘6’
push ‘6’
pop ‘6’
6 4 2
push ‘2’
pop ‘4’
push ‘6’
pop ‘6’
push ‘4’
pop ‘6’
Now, If the other array is [2,4,6], [2,6,4], [4,2,6], [4,6,2], or [6,4,2] then the answer is “YES” otherwise “NO”.
Note:
Please note that the arrays will only contain unique elements.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain an integer ‘N’ which represents the total number of elements in both arrays.
The second line of each test case contains the ‘N’ space-separated integers which represent the elements of the 'FIRST' array.
The third line of each test case contains the ‘N’ space-separated integers which represent the elements of the 'OTHER' array.
Output Format:
For each test case, print “YES” if the first array is a valid stack permutation of the other. Otherwise, print “NO”.
Note:
You don’t need to print anything; It has already been taken care of.
1 <= T <= 10
1 <= N <= 10000
0 <= FIRST[i], OTHER[i] <= 10^5
Where 'FIRST[i]' and 'OTHER[i]' denote the value of the i-th element of the input arrays.
Time limit: 1 sec
2
3
2 4 6
4 6 2
3
2 4 6
6 2 4
YES
NO
In test case 1, The explanation is already given in the example.
In test case 2, The explanation is already given in the example.
2
3
2 4 6
2 3 4
1
5
5
NO
YES
In test case 1, the 'FIRST' array does not contain 3 but the 'OTHER' array contains 3. So, the other is not the valid stack permutation.
In test case 2, the only array containing ‘5’ as an element is the valid stack permutation of the array [5].
Follow the order of elements in the first array by considering the push and pop operations.
Follow the order of elements in the first array by considering the push and pop operations.
The basic idea is to iterate through all the elements of the first array and push each element in a stack. You also have to pop the elements from the stack with respect to the occurrence of elements in the other array, so as to maintain the order of push and pop operations.
The Steps are as follows:
O(N), Where ‘N’ is the length of the given arrays.
Since we are iterating through the array once the worst-case time complexity will be O(N). We are here only doing N push/pop operations because of that the overall time complexity will be O(N).
O(N)
Since we are storing each element in a stack which results in O(N) space complexity in the worst case. So, the overall space complexity will be O(N).