Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,145 views

in Online Assessments by Expert (143,740 points) | 1,145 views

1 Answer

0 like 0 dislike
Input 
6
2
1
0
output 
0
1
2
3
4
5

Explanation
Here N=6, K=2 and P=1. No position is Forbidden. Initially A=[1,0,0,0,0,0]. We can reach 2 in 
one move by reverse subarray[1:2] which makes A=[0,1,0,0,0,0]. Similarly again reversing 
subarray [2:3] makes A=[0,0,1,0,0,0]. SImilarly we keep reversing one by one until the 1 
reaches the last of subarray.

------------------------------------------------------------------------------------------------

Input
4
1
2
1
3

Output
-1
0
-1
-1

Explanation
Here N=4, K=1 and P=2. Position 3 is Forbidden. Initially A=[0,1,0,0]. SInce K=1 reversing the 
subarray wont do any change hence we cannot make any position 1 except the starting one.

------------------------------------------------------------------------------------------------------
Input
10
4
3
3
2
5
10

Output
2
-1
0
1
-1
1
2
3
2
-1

Explanation
N=10 K=4 P=3 M=3 B=[2,5,10] Initially A=[0,0,1,0,0,0,0,0,0,0] For the first position: reverse a 
subarray [2,3,4,5] after this A will be [0,0,0,1,0,0,0,0,0,0] and now reverse the subarray [1,2,3,4] 
so 1 will come at the first position in 2 operations. Note that we did not move 1 on any 
forbidden position during the operations. Initially, 1 was on positions 3 then came to position 
4 and at the came at position 1. Since 2, 5, and 10 are forbidden positions hence the number 
of steps for these positions will be -1. Initially, 1 is on the 3rd position hence the number of 
steps for the 3rd position is 0. For the 8th position: Reverse [3,4,5,6] o 1 will be on the 6th 
position now reverse [6,7,8,9] so 1 will be on the 9th position now reverse [7,8,9,10] so 1 will 
be the position. Thus, in 3 operations one can reach 8th position without any forbidden cell. 
Thus Arr={2,-1,0,1,-1,1,2,3,2,-1}
------------------------------------------------------------------------------------------------------
by Expert (143,740 points)

Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .