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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:34:18+00:00 2026-06-16T06:34:18+00:00

I found the following problem when preparing for an interview: 3 can be written

  • 0

I found the following problem when preparing for an interview:

3 can be written as 1+1+1, 1+2, 2+1; 4 can be written as 1+1+1+1, 1+1+2, 2+2, 1+2+1, 2+1+1, 3+1, 1+3; Given an integer, how many possible expressions exist? (1+2 and 2+1 are different)

So this is fairly straightforward to write a brute force algorithm to calculate that and get the set all the sets of numbers that do it:

private static Set<List<Integer>> getIntegersSum(int num) {
  Set<List<Integer>> returnSet = new HashSet<List<Integer>>();

  if (num == 0) {
   returnSet.add(new LinkedList<Integer>());
   return returnSet;
  }

  for (int i = 1; i <= num; i++) {
    Set<List<Integer>> listNMinI = getIntegersSum(num - i);
    for (List<Integer> l : listNMinI) {
      l.add(0, i);
      returnSet.add(l);
    }
  }

  return returnSet;
}

Now I believe the recurrence relation that describes the complexity of this algorithm is:

T(0) = \Theta (1)
T(n) = O(n) + \Sum_{i=0}^{n-1} T(i)

I’m not quite sure how to arrive at big-oh complexity from this recurrence relation. I would also like to know if there is a closed form solution for the question (how many combinations like that we will have for each number).

I’m also not sure what the complexity would be if we memoize this algorithm by caching the results of each call (similarly to how you can speed up fibonacci).

  • 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-16T06:34:19+00:00Added an answer on June 16, 2026 at 6:34 am

    There are 2n – 1 – 1 of such expressions.

    There are a number of ways to approach this problem.

    I use result of this problem:

    There are n candies. How many ways are there to split all n candies to k people (0 candy can be given)?

    The order is important, since the portions go to different people. The solution is (n + k – 1)C(k – 1). We add k – 1 separators into the mix (which makes the sum n + k – 1), and we try to find the number of ways to insert the separators to separate the candies into k parts. Think of n + k – 1 boxes in a line to place the candies and separators, and we want to find number of ways to choose k – 1 slots for the separators, which separates the boxes into k portions.


    Back to this problem, we need to answer this sub-problem:

    How many ways to express n as sum of k positive numbers?

    We can reuse the result from the candy splitting problem above, but we need to reserve k to prevent the terms to be 0. So the result will be ((n – k) + k – 1)C(k – 1), which simplifies to (n – 1)C(k – 1). (The (n – k) is due to we putting away k for each of the k terms).

    So the final result will be Sum [i = 2..n] (n – 1)C(i – 1), since the expression contains at least 2 terms, and at most n terms. We know that Sum [i = 1..n] (n – 1)C(i – 1) = 2n – 1, so Sum [i = 2..n] (n – 1)C(i – 1) = 2n – 1 – 1.

    Another way to approach this problem is explained by @MarkDickinson in the comment. The reasoning is more straight-forward.

    Between each pair of candies, either there’s a separator or there isn’t. That immediately gives 2^(n-1) possibilities. For whatever reason the OP is excluding the single case where we’ve got only 1 portion, so subtract 1 to get 2^(n-1) - 1.

    to make the argument more solid. Since the problem only allow positive terms, we can only insert 1 separator between the candies, and we can only insert them between the candies, not the 2 ends. Therefore, there are (n – 1) places that the separator can appear.

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

Sidebar

Related Questions

I can't found out how to solve the following problem: preg_replace(/e[^t]/, , testet); This
Take the following problem: how many numbers are in a given range of integers,
I did my research but haven't found an answer to the following problem: I
I found the following lines in a makefile tutorial, but I have some problem
I've faced the following problem. I'm developing a form for the site and this
While developing a site which I can't link to directly, I've found a following
I need some help if possible with the following problem.... atomsClause :: Clause !
I'm checking my code with JSHint and I found the following problem that I
I'm writing a concurrent transaction library in C and found the following problem. Let's
Can anybody help me to find out solution of following problem. In ASP.NET website:

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.