The Wayback Machine - https://web.archive.org/web/20220228212611/https://www.geeksforgeeks.org/represent-n-as-sum-of-maximum-number-of-unique-positive-integers/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Represent N as sum of maximum number of unique positive integers

  • Last Updated : 28 Feb, 2022

Given a number N, the task is to represent N as sum of maximum number of unique positive integers.

Examples: 

Input: N = 12
Output: 1+ 2 + 3 + 6
Explanation: 
Possible splits are: 
                              2 + 10 
                             4 + 8
                             2 + 4 + 6  
                            1 + 2 + 3 + 6
Among them, (1 + 2 + 3 + 6) contains the maximum number of unique integers.

Input: N = 6
Output: 1 + 2 + 3

 

Approach: Consider the below observations to solve the problem:

  • We know that any number N can be represented as the sum of N 1s.  
  • But it is required in this problem that no duplicate values are taken.
  • Hence we will try to group N 1s into unique combinations, which will surely give as sum N.

Illustration:

Consider, N = 12 = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1

Since the numbers have to be unique, lets try to group 1s in a continuous manner.

  • Cannot group 1’s in this manner since second grouping and last grouping result in same number.
    12 = (1) + (1 + 1) + (1 + 1 + 1) + (1 + 1 + 1 + 1) + (1 + 1)
        = 1 + 2 + 3 + 4 + 2
  • To avoid this, combine the last two groupings.
    12 = (1) + (1 + 1) + (1 + 1 + 1) + (1 + 1 + 1 + 1 + 1 + 1)
       = 1 + 2 + 3 + 6

Therefore, To make groupings unique, the number of 1’s in any two groups should not be the same.

Below is the implementation of the above approach: 

C++




// C++ program to find maximum number of
// splits into unique positive integers.
  
#include <iostream>
#include <vector>
using namespace std;
  
vector<int> maximumSplit(int N)
{
    vector<int> answer;
    int groupSize = 1;
  
    while (N >= groupSize) {
        answer.push_back(groupSize);
        N -= groupSize;
        groupSize++;
    }
  
    // In case N != 0, then it is not
    // possible to group remaining 1's
    if (N != 0) {
  
        // Get the last unique integer
        int last = answer.back();
        answer.pop_back();
  
        // Add the remaining 1's to it
        last += N;
  
        // Add the new number to list
        answer.push_back(last);
    }
  
    return answer;
}
  
// Driver Code
int main()
{
    int N = 12;
  
    vector<int> answer = maximumSplit(N);
    for (int n : answer)
        cout << n << " ";
}

Java




// Java program to find maximum number of
// splits into unique positive integers.
  
import java.util.*;
  
class GFG {
    public static List<Integer> maximumSplit(int N)
    {
        List<Integer> answer
            = new ArrayList<>();
        int groupSize = 1;
  
        while (N >= groupSize) {
            answer.add(groupSize);
            N -= groupSize;
            groupSize++;
        }
  
        // In case N != 0,
        // then we were not able
        // to group remaining 1's
        if (N != 0) {
  
            // get the last unique integer
            int last
                = answer.remove(
                    answer.size() - 1);
  
            // add the remaining 1's to it
            last += N;
  
            // add the new number to list
            answer.add(last);
        }
  
        return answer;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int N = 12;
  
        List<Integer> answer = maximumSplit(N);
        for (int n : answer)
            System.out.print(n + " ");
    }
}
Output
1 2 3 6

Time complexity: O(N)
Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!