Find the number on the card that remains in the end after performing the given operations - GeeksforGeeks

15 Apr, 2021
Given an integer N which represents the number of cards in a deck. The deck is ordered from 1 to N where 1 is the topmost card and N is at the bottom. You take out the topmost card from the deck and insert it at the bottom and throw the next card that appears at the top of the deck. Again you do the same thing until a single card remains. The task is to find the number of the card that remains at the end.Examples:  
Input: N = 4 
Output: 1
1 2 3 4
^     ^
Top   Bottom

Operation 1: 3 4 1 (1 got shifted to the bottom and 2 got removed)
Operation 2: 1 3 (3 got shifted and 4 got removed)
Operation 3: 1 (3 got removed after shifting 1)

Input: N = 10
Output: 5
 
Approach:  
First of all insert numbers from 1 to N in a queue.Now, dequeue the front element from the queue and enqueue it at the end.Finally, pop the element at the front.Print the final element left in the queue.
First of all insert numbers from 1 to N in a queue.
Now, dequeue the front element from the queue and enqueue it at the end.
Finally, pop the element at the front.
Print the final element left in the queue.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function that will find the// card number which is remainingint remainingCard(int n){    queue<int> queCards;     // Inserting all the numbers from 1 to n    for (int i = 1; i <= n; i++)        queCards.push(i);     // While there are atleast two    // elements in the queue    while (((int)queCards.size()) >= 2) {         // Push the front element at the back        queCards.push(queCards.front());         // Remove the front element        queCards.pop();         // Remove another element        queCards.pop();    }     // Return the only element    // left in the queue    return queCards.front();} // Driver codeint main(){    int n = 10;     cout << remainingCard(n);     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{ // Function that will find the// card number which is remainingstatic int remainingCard(int n){    Queue<Integer> queCards = new LinkedList<>();     // Inserting all the numbers from 1 to n    for (int i = 1; i <= n; i++)    {        queCards.add(i);    }     // While there are atleast two    // elements in the queue    while (((int) queCards.size()) >= 2)    {         // Push the front element at the back        queCards.add(queCards.peek());         // Remove the front element        queCards.remove();         // Remove another element        queCards.remove();    }     // Return the only element    // left in the queue    return queCards.peek();} // Driver codepublic static void main(String[] args){    int n = 10;     System.out.println(remainingCard(n));}} // This code is contributed by PrinciRaj1992
# Python3 implementation of the approach # Function that will find the# card number which is remainingdef remainingCard(n):     queCards = []     # Inserting all the numbers from 1 to n    for i in range(1, n + 1):        queCards.append(i)     # While there are atleast two    # elements in the queue    while (len(queCards) >= 2):         # Push the front element at the back        queCards.append(queCards[0]);         # Remove the front element        queCards.pop(0);         # Remove another element        queCards.pop(0);     # Return the only element    # left in the queue    return queCards[0] # Driver coden = 10print(remainingCard(n)) # This code is contributed by divyamohan123
// C# implementation of the approachusing System;using System.Collections.Generic; class GFG{ // Function that will find the// card number which is remainingstatic int remainingCard(int n){    Queue<int> queCards = new Queue<int>();     // Inserting all the numbers from 1 to n    for (int i = 1; i <= n; i++)    {        queCards.Enqueue(i);    }     // While there are atleast two    // elements in the queue    while (((int) queCards.Count) >= 2)    {         // Push the front element at the back        queCards.Enqueue(queCards.Peek());         // Remove the front element        queCards.Dequeue();         // Remove another element        queCards.Dequeue();    }     // Return the only element    // left in the queue    return queCards.Peek();} // Driver codepublic static void Main(String[] args){    int n = 10;     Console.WriteLine(remainingCard(n));}} // This code is contributed by Princi Singh
<script> // Javascript implementation of the approach // Function that will find the// card number which is remainingfunction remainingCard(n){    queCards = [];     // Inserting all the numbers from 1 to n    for (var i = 1; i <= n; i++)        queCards.push(i);     // While there are atleast two    // elements in the queue    while ((queCards.length) >= 2) {         // Push the front element at the back        queCards.push(queCards[0]);         // Remove the front element        queCards.shift();         // Remove another element        queCards.shift();    }     // Return the only element    // left in the queue    return queCards[0];} // Driver codevar n = 10;document.write( remainingCard(n)); </script>
5
 
divyamohan123
princiraj1992
princi singh
rrrtnx
Queue
Queue
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Construct Complete Binary Tree from its Linked List Representation
FIFO (First-In-First-Out) approach in Programming
Reversing a Queue
Applications of Priority Queue
Iterative Method to find Height of Binary Tree
Circular Queue | Set 2 (Circular Linked List Implementation)
Reversing the first K elements of a Queue
Sort the Queue using Recursion
How to remove a specific element from Queue
Priority queue of pairs in C++ with ordering by first and second element