Ticket Counter | Practice | GeeksforGeeks

N people standing in the queue of a movie ticket counter. It is a weird counter, distributes tickets to first K people and then last K people and again first K people and so on. The task is to find the last person to get the ticket.

Example: Let N = 9, K = 3, starting queue will like {1, 2, 3, 4, 5, 6, 7, 8, 9}. After the first distribution queue will look like {4, 5, 6, 7, 8, 9}. And after the second distribution queue will look like {4, 5, 6}. The last person to get the ticket will be 6. 
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains two space-separated integers N and K.

Output: For each test case, print the last problem which remains

Constraints:
1. 1 <= T <= 10
2. 1 <= K<= N <= 105

Example:
Input:
2
9 3
25 7

Output:
6
15
0
msbritogabriella2 months ago
Easy C++ solution using Deque
0
soumik8673 months ago
//////Using only vector
#include<iostream>#include<bits/stdc++.h>using namespace std;int main(){int t;cin>>t;while(t--){    int n,k;    cin>>n>>k;    vector<int> v(n);    v[0]=1;    if(n==1){        cout<<1<<endl;        return 0;    }    for(int i=1;i<n;i++){        v[i]=v[i-1]+1;    }    int x=0,y=n-1,check=0,check1=0;    while((y-x)>=k){        x+=k;        if((y-x)<=k){            check=1;            break;        }        y-=k;        if((y-x)<=k){            check1=1;            break;        }    }    if(check==1) cout<<v[x]<<endl;    if(check1==1) cout<<v[y]<<endl;}return 0;}
0
aroopghosh5533 months ago
#include <bits/stdc++.h>
using namespace std;

int last(int n, int k){
    deque<int> dq;
    for(int i = 1; i <= n; i++){
        dq.push_back(i);
    }
    int i = 1; 
    while(dq.size() > 1){
        if(i % 2 != 0){
            for(int j = 0; j < k; j++){
                if(dq.size() == 1){
                    return dq.front();
                }
                dq.pop_front();
            }
            i++;
        }
        else{
            for(int j = 0; j < k; j++){
                if(dq.size() == 1){
                    return dq.front();
                }
                dq.pop_back();
            }
            i++;
        }
    }
    return dq.front();
}

int main() {
	int t;
	cin >> t;
	while(t--){
	    int n, k;
	    cin >> n >> k;
	    cout << last(n, k) << "\n";
	}
	return 0;
}
0
avinav26113 months ago
Easy C++ Solution using Set
 
0
kewatshubham993 months ago
solution in 0.2/1.8
/*package whatever //do not write package name here */

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
	public static void main (String[] args) {
		Scanner scan  = new Scanner(System.in);
		int test = scan.nextInt();
		
		for(int k=0;k<test;k++){
	        int total = scan.nextInt();
	        int pass = scan.nextInt();
	        
	        int i=0,j=total;
	        
	        while(j>i||i<j){
	            i+=pass;
	            if(i>=j){
	                System.out.println(j);break;
	            }
	            
	            j-=pass;
	            if(j<=i){
	                System.out.println(i+1);break;
	            }
	                
	        }
		}//code
		
		
	}
}
solution in 0.2/1.8
0
imranwahid6 months ago
Easy C++ solution
https://ide.geeksforgeeks.org/5Fo5gOqvC5
#include<bits/stdc++.h>
using namespace std;
int helper(int n,int k)
{
    int start=1,end=n;
    bool front=true;
    while(end-start+1>k)
    {
        if(end-start+1>k)
        {
            start+=k;
            front=false;
        }
        else
        {
            break;
        }
        if(end-start+1>k)
        {
            end-=k;
            front=true;
        }
        else
        {
            break;
        }
    }
    return front?end:start;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	    int n,k;
	    cin>>n>>k;
	    cout<<helper(n,k)<<endl;
	}
	return 0;
}
0
syedabdullah2736 months ago
#include <iostream>#include <deque>using namespace std;
int main() {//code   int T,N,K;deque<int>people;cin>>T;while(T--){    cin>>N;    cin>>K;        for(int i=1;i<=N;i++)     people.push_back(i);          while(people.size()>1)     {    for(int i=1;i<=K&&people.size()>1;i++)     people.pop_front();       for(int i=1;i<=K&&people.size()>1;i++)     people.pop_back();     }         cout<<people.front()<<endl;        people.pop_front();       } return 0;}
0
abhishekpanwar6976 months ago
#include <bits/stdc++.h>using namespace std;
int main() {int t;cin>>t;while(t--){int n,k;cin>>n>>k;deque<int>q;for(int i=1;i<=n;i++){   q.push_back(i);}int flag=1,last;while(!q.empty()){   if(flag==1)   {       int x=k;       while(x&&!q.empty())       {           last=q.front();           q.pop_front();           x--;       }   }   else   {              int x=k;       while(x&&!q.empty())       {           last=q.back();           q.pop_back();           x--;       }   }   flag^=1;}cout<<last<<endl;}return 0;}
0
M G8 months ago
M G
Without using a deque
0
Prasad8 months ago
Prasad
can we follow another approach aspect deque
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.