X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Unique partitions
Medium Accuracy: 63.69% Submissions: 742 Points: 4

Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.

Example 1:

Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3, 
{3}, {2, 1} and {1, 1, 1}.

 

Example 2:

Input: n = 4 
Output: ​4 3 1 2 2 1 1 1 1 
Explanation: For n = 4, 
{4}, {3, 1}, {2, 2}, {1, 1, 1, 1}.

 

Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. 
Note: The generated output is printed without partitions. 

 

Expected Time Complexity: O(2^n)
Expected Space Complexity: O(n)


 

Constraints:
1 <= n <= 30

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Unique partitions

Output Window