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 8706667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:40:12+00:00 2026-06-13T03:40:12+00:00

Here is the code to a subset problem with arguments n and k. n

  • 0

Here is the code to a subset problem with arguments n and k. n represents the total number of students and k represents the amount of the students I want to get out of n. The code attempts to give the number of possible combinations of pulling k number of students out of n number of students.

def subset(n, k): 
    if k == 0:
        return 1
    if n == k:
        return 1
    else:
        return subset(n-1, k-1) + subset(n-1, k)

I understand the first part of the recursive call, but I’m having trouble understanding the + subset(n-1, k) part. Can anyone explain this to me?

  • 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-06-13T03:40:13+00:00Added an answer on June 13, 2026 at 3:40 am

    The recursion is based on a simple observation, for which I will give a combinatorial argument, as to why it is true, rather than a mathematical proof through formulae.

    Whenever you choose k elements out of n, there are two cases:

    1. You choose element #n
    2. You don’t choose element #n

    Since these events are mutually exclusive, the total amount of combinations is given by the amount of combinations when choosing #n, and those when you don’t choose #n.

    Choosing element #n

    Since we have already chosen one element, we need only choose another k-1 elements. Also, since we have decided upon one element – as to whether it is included or not – already, we only need to consider the remaining n-1 elements.

    Thus, the amount of combinations for choosing element #n is given by

        subset(n - 1, k - 1)
    

    Not choosing element #n

    There are still k elements to choose, but since we have already made up our mind about element #n, there remain only n - 1 elements to choose from. Thus:

        subset(n - 1, k)
    

    The base case

    The recursion uses the fact, that we can usually differentiate between two situations, solutions where element n is part of that solution, and those where it is not.

    However, such a distinction can not always be made:

    • When choosing all elements (corresponding to case n == k in code below)
    • or when choosing no elements at all (corresponding to case k == 0 in code below)

    In these cases, there is only exactly one solution, hence

    if k == 0:
        return 1
    if n == k:
        return 1
    

    Ensuring it works

    To do that, we need to convince ourselves (or prove) that the base case is always hit at some point.

    Let us assume, that n < k at some point. Since per our assumption, n was originally greater or equal to k, there must have been some point where n = k, because n and k decrease in unison or only n decreases by one, i.e. it follows

    This implies, that there must have been a call to subset(n - 1, k) for it to happen, that n decreases below k. However, this is not possible since we have a base case on n = k where we return a constant 1.

    We conclude that either n decreases at some point such that n = k, or decrease in unison exactly k times such that k = 0.

    Thus, the base case works.

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

Sidebar

Related Questions

enter code here Want to remove items from a Main list , but give
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
My code here works fine to get the content length, and tells me how
The script is on jsfiddle here : CODE What it does at the moment
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here is code my jqgrid editing through form. $(#DataEnergy).jqGrid('navGrid', '#pagergrid', {}, //options {editdata: {
Here my code, I need to insert into mysql database I have mysql,php,android 1)
Here is code from MSDN . I don't understand why the work isn't just
here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
Here is code example class A{ int i; public: A(int i) : i(i) {}

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.