Challenge: Reverse Game

Subdomeniu: Fundamentals (fundamentals)

Scor cont: 30.0 / 30

Submission status: Accepted

Submission score: 1.0

Submission ID: 464726749

Limbaj: cpp14

Link challenge: https://www.hackerrank.com/challenges/reverse-game/problem

Cerinta

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 $0^{th}$ position, then from $1^{st}$ position, then from $2^{nd}$ 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.

**Input Format**  
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$. 

**Output Format**  
Print the final index of ball $K$ in the array.  

**Constraints**  
$1 \le T \le 50$  
$1 \le N \le 10^5$    
$0 \le K < N$

**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<br>
So, Index of 1 will be 2.

Cod sursa

#include <iostream>

using namespace std;

int main()
{
    int t, n, k;
    cin >> t;
    
    for (int i = 0; i < t; i++) {
        cin >> n >> k;     
        k <= n / 2 - 1 ? cout << 2 * k + 1 << endl : cout << 2 * (n - 1 - k) << endl; 
    }

    return 0;
}
HackerRank Fundamentals – Reverse Game