Generalized N-Cards Problem - MATLAB Cody - MATLAB Central

Problem 272. Generalized N-Cards Problem

Difficulty:Rate

Preface: This is a generalized version of the problem I presented here.

You have a deck of N cards numbered in order from 1 to N. You are given a pattern to discard a certain number of cards and move a number of cards to the bottom of the deck. Eventually, you will have one card left. What is the number of that card?

The sequence of discarding/moving cards will be given in a row vector of 1s and -1s where 1 represents moving a card to the bottom of the deck and -1 represents discarding a card. This sequence should be repeated until there is only one card left (e.g. [-1 1] would return this problem to the non-generalized case).

Example

    generalNCardsProblem(5,[-1 1])
    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 ]
    generalNCardsProblem(5,[-1 1 1 -1])
    deck = [ 1 2 3 4 5 ]
    deck = [ 2 3 4 5 ]
    deck = [ 3 4 5 2 ]
    deck = [ 4 5 2 3 ]
    deck = [ 5 2 3 ]
    deck = [ 2 3 ]
    deck = [ 3 2 ]
    deck = [ 2 3 ]
    deck = [ 3 ]

Solution Stats

60.0% Correct | 40.0% Incorrect
Last Solution submitted on May 14, 2025

Problem Comments

Solution Comments

Show comments
PIVlab surpasses 100K all-time File Exchange downloads
During the past twelve months, PIVlab, a MATLAB Community Toolbox for particle...
4
8
LLMs with MATLAB updated to support the latest OpenAI Models
Large Languge model with MATLAB, a free add-on that lets you access...
2
4

Problem Recent Solvers32

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!