Algorithm


Problem Name: Mathematics - Reverse Game

Problem Link: https://www.hackerrank.com/challenges/reverse-game/problem?isFullScreen=true

In this HackerRank in Mathematics - Reverse Game solutions,

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.

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 <= T <= 50

1 <= N <= 105

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

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    int t,n,k,i,l;
scanf("%d",&t);
while(t--){
    scanf("%d%d",&n,&k);
    l = n/2;
        if(k < n/2>
            i=2*k+1;
        else if (k>=n/2)
         i = 2*(n-1-k);
    printf("%d\n",i);
}
return 0;
}
Copy The Code & Try With Live Editor

#2 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector < string> split(const string &);



int main(){
    int t;
    cin >> t;
    while (t--){
        int n, k;
        cin >> n >> k;
        
        if (k >= (n-1)/2){
            cout << 2*(n-1-k) << endl;
        }else{
            cout << 2*k+1 << endl;
        }
    }
    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun < int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun < int, int>(isspace))).base(),
        s.end()
    );

    return s;
}

vector < string> split(const string &str) {
    vector<string> tokens;

    string::size_type start = 0;
    string::size_type end = 0;

    while ((end = str.find(" ", start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));

        start = end + 1;
    }

    tokens.push_back(str.substr(start));

    return tokens;
}
Copy The Code & Try With Live Editor

#3 Code Example with Java Programming

Code - Java Programming


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;



public class Solution {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt()-1, k = sc.nextInt();
            if(k==n/2)
                System.out.println(2*k);
            else if(k<=n/2)
                System.out.println(2*k+1);
            else
                System.out.println(2*(n-k)>;
        }
    }
}
Copy The Code & Try With Live Editor

#4 Code Example with Python Programming

Code - Python Programming


#!/bin/python3

import math
import os
import random
import re
import sys



def reverse_game(N: int, K: int):
    i, j, lst = N - 1, 0, list()
    lst += [i, j]
    while i + j == N - 1 and i > j:
        i -= 1; lst.append(i)
        j += 1; lst.append(j)
    return lst[:N].index(K)


if __name__ == '__main__':
    t = int(input().strip())
    
    for _ in range(t):
        n, k = map(int, input().split())
        print(reverse_game(n, k))
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Restaurant solution in Hackerrank - Hacerrank solution C, C++, C#, java, js, Python
Next
[Solved] Strange Grid Again solution in Hackerrank - Hacerrank solution C, C++, C#, java, js, Python