Given an integer N, the task is to find N distinct integers whose sum is N. If there is more than one combination of the integers, print any one of them.
Input: N = 3
Output: 1, -1, 3
On adding the numbers that is 1 + (-1) + 3 the sum is 3.
Input: N = 4
Output: 1, -1, 0, 4
On adding the numbers that is 1 + (-1) + 0 + (4) the sum is 4.
Approach: The idea is to print N/2 Symmetric Pairs like (+x, -x) so that the resultant sum will always be 0.
Now if integer N is odd, then print N along with these set of integers to make sum of all integers equals to N
If N is even, print 0 and N along with these set of integers to make sum of all integers equals to N.
Below is the implementation of the above approach:
-1, 1, -2, 2, 0, 5
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.
- Find N distinct integers with zero sum
- Find any K distinct odd integers such that their sum is equal to N
- Find K distinct positive odd integers with sum N
- Find distinct integers for a triplet with given product
- Number of distinct integers obtained by lcm(X, N)/X
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Check if the sum of distinct digits of two integers are equal
- Check whether a number can be represented as sum of K distinct positive integers
- Integers from the range that are composed of a single distinct digit
- Maximum number of distinct positive integers that can be used to represent N
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Find K consecutive integers such that their sum is N
- Find the prime P using given four integers
- Find if it is possible to choose subarray that it contains exactly K even integers
- Find the first N integers such that the sum of their digits is equal to 10
- Find N integers with given difference between product and sum
- Find three integers less than or equal to N such that their LCM is maximum
- Find sum in range L to R in given sequence of integers
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Find pair with maximum GCD for integers in range 2 to N
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.