Akash and Akhil are playing a game. They have N balls numbered from 0 to N−1. Akhil asks Akash to reverse the position of the balls, i.e., to change the order from say, 0,1,2,3 to 3,2,1,0. He further asks Akash to reverse the position of the balls N times, each time starting from one position further to the right, till he reaches the last ball. So, Akash has to reverse the positions of the ball starting from 0th position, then from 1st position, then from 2nd position and so on.
At the end of the game, Akhil will ask Akash the final position of any ball numbered K. Akash will win the game, if he can answer. Help Akash.
The first line contains an integer \$T\$, i.e., the number of the test cases.
The next \$T\$ lines will contain two integers \$N\$ and \$K\$.
Print the final index in array.
- \$1 ≤ T ≤ 50\$
- \$1 ≤ N ≤ 10^5\$
2 3 1 5 2
For first test case, The rotation will be like this: 0 1 2 -> 2 1 0 -> 2 0 1 -> 2 0 1 So, Index of 1 will be 2.
test = gets.chomp.to_i test.times do num, numbered = gets.split.map(&:to_i) a =  elem = 0 a << elem (num-1).times do elem += 1 a << elem end len = a.length final =  w =  len.times do if w.length == 0 q = a.reverse else q = w.reverse end elem = q.shift final << elem w = q - [elem] end puts final.index(numbered) end
I could only pass 4 test cases out of 11 since it always timed out for the rest. This is happening because my solution isn't efficient enough. How can we do it efficiently so that it passes all 11 test cases?