Given an integer N, our task is to print N distinct numbers such that their sum is 0.
Input: N = 3
Output: 1, -1, 0
On adding the numbers that is 1 + (-1) + 0 the sum is 0.
Input: N = 4
Output: 1, -1, 2, -2
On adding the numbers that is 1 + (-1) + 2 + (-2) the sum is 0.
Approach: To solve the problem mentioned above the main idea is to print Symmetric Pairs like (+x, -x) so that the sum will always be 0. The edge case to the problem is to observe that if integer N is odd, then print one 0 along with the numbers so that sum is not affected.
Below is the implementation of the above approach:
1, -1, 2, -2, 3, -3, 4, -4, 5, -5,
Time Complexity: O(log N)
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.
- Count numbers with exactly K non-zero digits and distinct odd digit 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 N distinct integers with sum N
- Check whether product of integers from a to b is positive , negative or zero
- Minimize cost to convert given two integers to zero using given operations
- 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
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Find distinct integers for a triplet with given product
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Number of distinct integers obtained by lcm(X, N)/X
- 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
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Check if a number can be represented as sum of non zero powers of 2
- Minimum steps to make sum and the product of all elements of array non-zero
- Find the number of consecutive zero at the end after multiplying n numbers
- Find all possible triangles with XOR of sides zero
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.