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

  • SEARCH
  • Home
  • 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 8970945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:50:02+00:00 2026-06-15T17:50:02+00:00

Whenever I see a recursive solution, or I write recursive code for a problem,

  • 0

Whenever I see a recursive solution, or I write recursive code for a problem, it is really difficult for me to figure out the time complexity, in most of the cases I just say its exponential? How is it exponential actually? How people say it is 2^n, when it is n!, when it is n^n or n^k.

I have some questions in mind –

  1. let say find all permutations of a string (O(n!))
  2. find all sequences which sum up to k in an array (exponential, how exactly do I calculate).
  3. Find all subsets of size k whose sum is 0 (will k come somewhere in complexity , it should come right?).

Can any1 help me how to calculate the exact complexity of such questions, I am able to wrote code for them, but its hard understanding the exact time complexity.

  • 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-15T17:50:03+00:00Added an answer on June 15, 2026 at 5:50 pm

    find all sequences which sum up to k in an array (exponential, how exactly do I calculate).

    Let F(a, n, k) be the number of all subsets of S ⊂ {0, 1, ..., n-1} so that

     ∑ a[i] == k
    i∈S
    

    Then we can compute F(array, length of array, k) recursively by splitting the problem in two subproblems (for n > 0).

    The subsets of {0, 1, ..., n-1} can be partitioned into two classes, those that contain n-1 and those that don’t.

    We obtain the recursion

    F(a,n,k) = F(a,n-1,k) + F(a,n-1, k-a[n-1])
    

    Let T(n) be the time necessary to compute F(_,n,_) (the underscores indicating that T(n) does only depend on n, not on the array or on k [although for specific arrays and k faster algorithms are possible]. The recursion for F then immediately implies

    T(n) = 2 * T(n-1)
    

    for n > 0.

    For n == 0, we can compute the solution in constant time,

    F(a, 0, k) = k == 0 ? 1 : 0
    

    so we have T(n) = 2^n * T(0) inductively.

    If the subsets shall not only be counted, but output, the complexity becomes O(n * 2^n) and that bound is tight (for an array of all 0s, with k == 0, all subsets meet the condition, and printing them takes Θ(n * 2^n) time).

    Find all subsets of size k whose sum is 0 (will k come somewhere in complexity , it should come right?).

    Yes, the complexity of that problem depends on n and k.

    Let F(a,n,k,s) be the number of subsets S ⊂ {0, 1, ..., n-1} of cardinality k such that

     ∑ a[i] == s
    i∈S
    

    For k == 0, we again have a constant time answer, there is one such subset (the empty set) if s == 0, and none otherwise. For k > n the set {0, 1, ..., n-1} has no subsets of cardinality k, so F(a,n,k,s) = 0 if k > n.

    If 0 < k <= n, we can, like above, consider the subsets containing n-1 and those that don’t separately, giving

    F(a,n,k,s) = F(a,n-1,k,s) + F(a,n-1,k-1,s - a[n-1])
    

    and for the time complexity we find

    T(n,k) = T(n-1,k) + T(n-1,k-1)
    

    That recursion is known from the binomial coefficients, and we have

    T(n,k) = n `choose` k = n! / (k! * (n-k)!)
    

    (with T(n,0) = 1).

    Once again, if the sets shall not only be counted, but output, the time complexity increases, here all sets have cardinality k, so it becomes

    k * n! / (k! * (n-k)!)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whenever I see a problem that would be shared by others, with a solution
I want code to run whenever I create a new object. For example, see
Whenever I see Ruby code that says: arrayNames.collect { ... } I forget what
Every now and then whenever i see any code for plugin, i could not
Is it possible to see which files have been changed whenever an change-event is
Whenever any question is asked, and a reference text is needed, I never see
Whenever I load a solution in Visual Studio with a specific project set as
Whenever a model is created in my application, an email is sent out to
Whenever I see images in an RSS feed, they are embedded in CDATA, rather
Whenever I see it being talked about, it sounds like one simply 'turns on'

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.