Represent N as sum of maximum number of unique positive integers
Last Updated :
07 Mar, 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++
#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++;
}
if (N != 0) {
int last = answer.back();
answer.pop_back();
last += N;
answer.push_back(last);
}
return answer;
}
int main()
{
int N = 12;
vector< int > answer = maximumSplit(N);
for ( int n : answer)
cout << n << " " ;
}
|
Java
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++;
}
if (N != 0 ) {
int last
= answer.remove(
answer.size() - 1 );
last += N;
answer.add(last);
}
return answer;
}
public static void main(String[] args)
{
int N = 12 ;
List<Integer> answer = maximumSplit(N);
for ( int n : answer)
System.out.print(n + " " );
}
}
|
Python3
def maximumSplit(N):
answer = [];
groupSize = 1 ;
while (N > = groupSize):
answer.append(groupSize);
N - = groupSize;
groupSize = groupSize + 1 ;
if (N ! = 0 ):
last = answer[ len (answer) - 1 ];
answer.pop();
last + = N;
answer.append(last);
return answer;
N = 12 ;
answer = maximumSplit(N);
for i in range ( len (answer)):
print (answer[i], end = " " );
|
C#
using System;
using System.Collections;
class GFG
{
static ArrayList maximumSplit( int N)
{
ArrayList answer = new ArrayList();
int groupSize = 1;
while (N >= groupSize) {
answer.Add(groupSize);
N -= groupSize;
groupSize++;
}
if (N != 0) {
int last = ( int )answer[answer.Count - 1];
answer.RemoveAt(answer.Count - 1);
last += N;
answer.Add(last);
}
return answer;
}
public static void Main()
{
int N = 12;
ArrayList answer = maximumSplit(N);
foreach ( int n in answer)
Console.Write(n + " " );
}
}
|
Javascript
<script>
const maximumSplit = (N) => {
let answer = [];
let groupSize = 1;
while (N >= groupSize) {
answer.push(groupSize);
N -= groupSize;
groupSize++;
}
if (N != 0) {
let last = answer[answer.length - 1];
answer.pop();
last += N;
answer.push(last);
}
return answer;
}
let N = 12;
let answer = maximumSplit(N);
for (let n in answer)
document.write(`${answer[n]} `);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...