Hey everyone, creating this thread to discuss the interview problem - Array Snap.
Practise this interview question on Coding Ninjas Studio (hyperlinked with the following link): Array Snap
Problem of the day
Ninja came across this exciting operation called Array Snap.
We can perform Array Snap on an array ‘X’ as follows:
Take any prefix of ‘X’ of any length and append it to the end. But the length of the prefix must not be ‘0’, and the prefix must not be equal to ‘X’.
For example ‘X’ = [2, 3, 1, 5, 6] and we want to perform array snap for prefix of length 3 then ‘X’ after operation will be [5, 6, 2, 3, 1].
Ninja has two arrays, ‘A’ and ‘B’, both of length ‘N’, determining how many different ways to transform ‘A’ into ‘B’ using Array Snap operation exactly ‘K’ times on ‘A’. The answer can be huge, so return it modulo 10^9+7.
Two ways are considered different if the order of applied operations differs. We can say that the two ways are different if there exists an ‘i’ such that (1 <= ‘i’ <= ‘K’) for the first way, we use the prefix of length ‘r’, and for the second way, we use the prefix of length ‘s’ and ‘r’ != ‘s’.
Example :‘N’ = 3, ‘A’ = [1, 2, 1], ‘B’ = [2, 1, 1], ‘K’ = 2
Only, 1 way to transform ‘A’ into ‘B’.
‘A’ = [1, 2, 1], select prefix of length 2 for array snap.
After 1st operation:
‘A’ = [1, 1, 2], select prefix of length 2 for array snap.
After 2nd operation:
‘A’ = [2, 1, 1].
This is the only possible way to transform ‘A’ into ‘B’.
Hence, the answer is 1.
The first line contains a single integer ‘T’ denoting the number of test cases, then the test case follows.
The first line of each test case contains two single space-separated integers, ‘N' and ‘K’.
Each test case's second and third line contains ‘N’ single space-separated integers representing ‘A’ and ‘B’.
Output Format :
For each test case, return the number of ways to transform ‘A’ into ‘B’ modulo 10^9+7.
Output for each test case will be printed on a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
1 ≤ N ≤ 1000
0 ≤ A[i], B[i] ≤ 10^9
0 ≤ K ≤ 10^5
It’s guaranteed that the sum of K over all the test cases does not exceed 10^5.
It’s guaranteed that the sum of N over all the test cases does not exceed 1000.
Time limit: 1 sec
2
2 9
1 2
2 1
6 1
1 2 7 1 2 7
2 7 1 2 7 1
1
2
For test case 1:
‘A’ = [1, 2], ‘B’ = [2, 1], ‘K’ = 1.
There is only one way to transform ‘A’ into ‘B’ using one array snap operation, and that is by performing an array snap-on prefix of length one on ‘A’.
Hence, the answer is 1.
For test case 2:
‘A’ = [1, 2, 7, 1, 2, 7], ‘B’ = [2, 7, 1, 2, 7, 1], ‘K’ = 1.
Here, there are two ways we can either perform array snap on the prefix of length one or the prefix of length 4.
‘A’ after array snap-on prefix of length one will be [2, 7, 1, 2, 7, 1].
‘A’ after array snap-on prefix of length four will also be the same.
2
3 0
1 2 3
1 2 3
2 10
4 10
10 4
1
0
How can we check if we can ever transform ‘A’ into ‘B’ with any number of operations?
Approach:
Algorithm:
O(4^K + N*N), ‘K’ is the number of operations to perform, and ‘N’ is the length of the given array.
We are making four recursive calls on every call. This will make the branching factor of our recursive tree four, and in the worst case, the height of our tree will be ‘K’ and ‘N*N’ for calculating the number of good and bad shifts.
Hence, the time complexity is O(4^K + N*N).
O(K + N), ‘K’ is the number of operations to perform, and ‘N’ is the length of the given array.
Only ‘K’ calls will be stacked in our function at any given moment because that is the height of our recursive tree.
Hence, the space complexity is O(K + N).
Interview problems
Discussion thread on Interview Problem | Array Snap
Hey everyone, creating this thread to discuss the interview problem - Array Snap.
Practise this interview question on Coding Ninjas Studio (hyperlinked with the following link): Array Snap