Technex Tickets
Source: codechef
Difficulty: 954

Technex Tickets
You are standing in a queue that is infinitely long, waiting to get tickets for various events during
Technex
.
The ticket distribution follows these rules:
Every second, tickets are given to the$1^{st}$and$3^{rd}$persons in the queue.
After receiving their tickets, those people leave the queue.
The person who was originally in the$2^{nd}$position (before the$1^{st}$and$3^{rd}$people left) moves up to the$1^{st}$position.
This process repeats every second, with the$1^{st}$and$3^{rd}$persons receiving tickets and leaving.
Initially, you are at position$N$in the queue.
Determine after how many seconds you will get the tickets.
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case contains a single line of input$N$your initial position in the line.
Output Format
For each test case, output on a new line after how many seconds will you get the tickets.
Constraints$1 \leq T \leq 1000$$1 \leq N \leq 1000$Sample 1:
Explanation:
At$1^{st}$second, person at position$1$and position$3$will get ticket.
At$2^{nd}$second, person at position$2$and position$5$will get ticket, as they are now at position$1$and$3$.
At$3^{rd}$second, person at position$4$and position$7$will get ticket.

Sample Input:
5
1
2
3
4
5

Sample Output:
1
2
1
3
2

C++ solution:
#include <bits/stdc++.h>
using namespace std;

int main() {
    // your code goes here
    int tc;
    cin>>tc;
    while(tc--){
        int n;
        cin>>n;
        if(n%2==0) cout<<(n/2)+1<<endl;
        else if(n==1) cout<<1<<endl;
        else cout<<(n/2)<<endl;
    }
    return 0;
}

Go solution:
package main

import "fmt"

func solve() {
    var h int
    fmt.Scan(&h) 

    var res []int

    for i := 0; i < h; i++ {
        var v int
        fmt.Scan(&v) 

        if v == 1 {
            res = append(res, 1)
        } else if v%2 == 0 { 
            res = append(res, v/2+1)
        } else { 
            res = append(res, (v-1)/2)
        }
    }

    for _, y := range res {
        fmt.Println(y)
    }
}

func main() {
    solve() 
}

Java solution:
import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef {
    public static void main(String[] args) throws java.lang.Exception {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int t = Integer.parseInt(br.readLine());
            while (t-- > 0) {
                int n = Integer.parseInt(br.readLine());
                System.out.println(solve(n));
            }
        } catch (Exception e) {
            // Handle potential exceptions
        }
    }

    static int solve(int n) {
        if (n == 1 || n == 3) {
            return 1;
        }
        if (n == 2) {
            return 2;
        }

        int seconds = 0;
        while (n > 3) {
            seconds++;
            n -= 2; // Two people leave (1st and 3rd effectively reduce our position by 2)
        }

        if (n == 1 || n == 3) {
            return seconds + 1;
        } else if (n == 2) {
            return seconds + 2;
        }
        return seconds; // Should not reach here based on the loop condition
    }
}

JavaScript solution:
process.stdin.setEncoding("utf-8");
let input = "";
process.stdin.on("data", function(nums){
    input +=  nums;
   
});
process.stdin.on("end", function(){
    let lines = input.trim("").split('\n');
    let T = parseInt(lines[0]);
            let A = [];
    for(let i = 1; i<=T; i++){
        let N = parseInt(lines[i]);
        let s = 0;

        while(N>3){
            N -= 2;
            s++;
        }
        if(N===1 || N===3){
            s+=1;
        }
        else{
            s+=2;
            
        }
    A.push(s);
    }
    console.log(A.join('\n'));
});

Python solution:
for _ in range(int(input())):
    pos=int(input())
    if  pos ==1:
        print(1)
    elif pos %2==0:
        print(int((pos/2)+1))
    else:
        print(int(pos/2))

Ruby solution:
def find_ticket_time(n)
  time = 0
  position = n

  loop do
    time += 1
    if position == 1 || position == 3
      return time
    end

    if position == 2
      position = 1
    elsif position > 3
      position -= 2
    end
  end
end

def main
  t = gets.chomp.to_i
  t.times do
    n = gets.chomp.to_i
    puts find_ticket_time(n)
  end
end

main