Given an integer N, the task is to find the maximum number of distinct positive integers that can be used to represent N.
Input: N = 5
5 can be represented as 1 + 4, 2 + 3, 3 + 2, 4 + 1 and 5.
So maximum integers that can be used in the representation are 2.
Input: N = 10
Approach: We can always greedily choose distinct integers to be as small as possible to maximize the number of distinct integers that can be used. If we are using the first x natural numbers, let their sum be f(x).
So we need to find a maximum x such that f(x) < = n.
1 + 2 + 3 + … n < = n
x*(x+1)/2 < = n
x^2+x-2n < = 0
We can solve the above equation by using quadratic formula X = (-1 + sqrt(1+8*n))/2.
Below is the implementation of the above approach:
Time Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Check whether a number can be represented as sum of K distinct positive integers
- Find K distinct positive odd integers with sum N
- Represent a number as the sum of positive numbers ending with 9
- Number of ways in which N can be represented as the sum of two positive integers
- Number of distinct ways to represent a number as sum of K unique primes
- Count of integers up to N which represent a Binary number
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Count positive integers with 0 as a digit and maximum 'd' digits
- Add two integers of different base and represent sum in smaller base of the two
- Number of arrays of size N whose elements are positive integers and sum is K
- Find optimal weights which can be used to weigh all the weights in the range [1, X]
- Check if given coins can be used to pay a value of S
- Count 'd' digit positive integers with 0 as a digit
- Ways to write N as sum of two or more positive integers | Set-2
- Find n positive integers that satisfy the given equations
- Check whether product of integers from a to b is positive , negative or zero
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.