The Reverse Game || Codechef Solution

 The Reverse Game :

Akash and Akhil are playing a game. They have  balls numbered from  to . 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  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  position, then from  position, then from  position and so on. At the end of the game, Akhil will ask Akash the final position of any ball numbered . Akash will win the game, if he can answer. Help Akash.

Input Format
The first line contains an integer , i.e., the number of the test cases.
The next  lines will contain two integers  and .

Output Format
Print the final index in array.

Constraints


Sample Input

2
3 1
5 2

Sample Output

2
4

Explanation
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.


  • Solution:
In C++ Language:

#include <iostream>

using namespace std;


int main() {

int t;

cin>>t;

while(t--) {

    int n,b;

    cin>>n>>b;

    

    int left = 0, right = n-1, i = right, dir = 1, ctr = 0;

    while(true) {

        if(b == i) {

            cout<<ctr<<endl;

            break;

        }

        ctr++;

        if(dir) {

            i = left;

            right--;

        }

        else {

            i = right;

            left++;

        }

        dir = !dir;

    }

}

return 0;

}


  • In Python 3.6 Language

for _ in range(int(input())):
    
    n,k=map(int,input().split())
    def finalarr(n):
        arr=[0]*n 
        arr[0]=n-1
        for i in range(1,n):
            if i%2:
                arr[i]=n-1-arr[i-1]
            else:
                arr[i]=n-2-arr[i-1]
      #  print(arr)
        return arr
    arr=finalarr(n)
    print(arr.index(k))
 




                                                          🙏THANKS FOR VISIT🙏
The Reverse Game || Codechef Solution The Reverse Game || Codechef Solution Reviewed by CodexRitik on November 06, 2020 Rating: 5

No comments:

Powered by Blogger.