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)
- Write a program to reverse digits of a number
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Minimum number of jumps to reach end
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Print all combinations of points that can compose a given number
- Check if a number is multiple of 5 without using / and % operators
- Median in a stream of integers (running integers)
- Given a number, find the next smallest palindrome
- Count the number of possible triangles
- Select a random number from stream, with O(1) space
- Program to convert a given number to words
- Efficient program to print all prime factors of a given number
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.