Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4005204
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:21:57+00:00 2026-05-20T08:21:57+00:00

How can I loop through all combinations of n playing cards in a standard

  • 0

How can I loop through all combinations of n playing cards in a standard deck of 52 cards?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:21:57+00:00Added an answer on May 20, 2026 at 8:21 am

    You need all combinations of n items from a set of N items (in your case, N == 52, but I’ll keep the answer generic).

    Each combination can be represented as an array of item indexes, size_t item[n], such that:

    • 0 <= item[i] < N
    • item[i] < item[i+1], so that each combination is a unique subset.

    Start with item[i] = i. Then to iterate to the next combination:

    • If the final index can be incremented (i.e. item[n-1] < N-1), then do that.
    • Otherwise, work backwards until you find an index that can be incremented, and still leave room for all the following indexes (i.e. item[n-i] < N-i). Increment that, then reset all the following indexes to the smallest possible values.
    • If you can’t find any index that you can increment (i.e. item[0] == N-n), then you’re done.

    In code, it might look something vaguely like this (untested):

    void first_combination(size_t item[], size_t n)
    {
        for (size_t i = 0; i < n; ++i) {
            item[i] = i;
        }
    }
    
    bool next_combination(size_t item[], size_t n, size_t N)
    {
        for (size_t i = 1; i <= n; ++i) {
            if (item[n-i] < N-i) {
                ++item[n-i];
                for (size_t j = n-i+1; j < n; ++j) {
                    item[j] = item[j-1] + 1;
                }
                return true;
            }
        }
        return false;
    }
    

    It might be nice to make it more generic, and to look more like std::next_permutation, but that’s the general idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I loop through all members in a JavaScript object, including values that
Using a for loop, how can I loop through all except the last item
How can I loop through all subviews of a UIView, and their subviews and
is there a way I can read in a namespace and loop through all
How can I loop through all the properties of object?. Right now I have
I wrote a code in C++ CLI which can loop through all files in
I can loop through all of the rows in a php script and do
Is there any way I can loop through all the dijit fields in a
How can i loop through all label elements in a div using jQuery or
How can you loop through all the words (spaces deliminate words) in an RTB

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.