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.
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
- 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:
- Minimum number of given operations required to make two strings equal
- Minimum operations required to make every element greater than or equal to K
- Find next right node of a given key
- Find the first N pure numbers
- Find the largest multiple of 3 | Set 1 (Using Queue)
- Find the sum of leafs at maximum level
- Find maximum level sum in Binary Tree
- Find the first non-repeating character from a stream of characters
- Find maximum vertical sum in binary tree
- Find the count of unvisited indices in an infinite array
- Find parent of each node in a tree for multiple queries
- Find the first circular tour that visits all petrol pumps
- Iterative Method to find Height of Binary Tree
- Queries to find the left-most given type integer in a binary array
- Nth number made up of odd digits only
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.