Problem 271. N-Cards Problem

You have a deck of N cards numbered in order from 1 to N. You discard the top card (card 1) and place the next card (card 2) at the bottom of the deck. Then you discard the top card of the remaining deck (card 3), and place the next card (card 4) at the bottom of the deck. We repeat the procedure: discarding the top card and placing the next card at the bottom of the deck. Eventually, you will have one card left. What is the number of that card?

Example

    nCardsProblem(5)
    deck = [ 1 2 3 4 5 ]
    deck = [ 2 3 4 5 ]
    deck = [ 3 4 5 2 ]
    deck = [ 4 5 2 ]
    deck = [ 5 2 4 ]
    deck = [ 2 4 ]
    deck = [ 4 2 ]
    deck = [ 2 ]

Solution Stats

53.49% Correct | 46.51% Incorrect
Last Solution submitted on Oct 13, 2023

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers150

Suggested Problems

More from this Author56

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!