Break Number

Moderate
0/80
Average time to solve is 35m
profile
Contributed by
15 upvotes
Asked in companies
AppleTata Consultancy Services (TCS)Deutsche Bank

Problem statement

Given a number 'N', you need to find all possible unique ways to represent this number as the sum of positive integers.

Note
1. By unique it is meant that no other composition can be expressed as a permutation of the generated composition. For eg. [1, 2, 1] and [1, 1, 2] are not unique.  

2. You need to print all combinations in non-decreasing order for eg. [1, 2, 1] or [1, 1, 2] will be printed as [1, 1, 2], however, the order of printing all the sequences can be random. 
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and the only line of the input contains an integer 'N' representing the given number.
Output Format:
Each line of the output contains one unique sequence which sums up to 'N'.

There will be 'K' lines of output containing one unique sequence on each line in non-decreasing order which sums up to 'N'. 'K' is the total number of unique sequences. 
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= N <= 50

Time Limit: 1sec
Sample Input 1:
4
Sample Output 1:
4
1 1 1 1 
1 1 2
2 2
1 3
Explanation For Sample Input 1:
Here notice that all combinations are sorted in non-decreasing order and [1, 1, 2] and [1, 2, 1] are the same and printed as [1, 1, 2]. 

Note: 1 1 1 1
      2 2
      4 
      1 3
      1 1 2  is also a valid output as the order of different sequences doesn’t matter.
Sample Input 2:
1
Sample Output 2:
1
Hint

Try brute force, try all possible combinations.

Approaches (2)
BruteForce
  1. The Naive approach uses a brute-force method to generate all the possible compositions possible by partitioning the given number
  2. For generating the combinations, let’s start from 1 because the minimum positive integer is 1. For every position, we will try all numbers from 1 to N, and solve the problem recursively for other positions.
  3. When we place a number at a position we decrease the remaining sum and move to the next position.
  4. Whenever our remaining sum = 0, we will store the sequence in an array, sort that array(to store sequence in nondecreasing order), and insert it into a set.
  5. We need to store each valid combination in a set so that we can avoid the duplicate combinations in our final answer [For example, the sequence [1,2,3] and [3,1,2] are the same, so we need to sort both sequences so that they both become [1,2,3] and the set will store only one instance of [1,2,3]. ]
  6. In the end, we will print all possible combinations.
Time Complexity

O((N^N)),  where N is the given number. 

 

As for each position, we are trying all possible numbers from 1 to N thus it takes O(N^N) complexity. 

Space Complexity

O(N) where N is the given number.

 

As we are using recursion, and recursion stack can use a maximum of N space at a time. Also, we are using a set data structure here it will also take linear space. Hence the space complexity will be O(N). 

Code Solution
(100% EXP penalty)
Break Number
Full screen
Console