Find N distinct integers with zero sum - GeeksforGeeks

25 Nov, 2021
Given an integer N, our task is to print N distinct numbers such that their sum is 0.Examples: 
Input: N = 3 Output: 1, -1, 0 Explanation: On adding the numbers that is 1 + (-1) + 0 the sum is 0.Input: N = 4 Output: 1, -1, 2, -2 Explanation: 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:  
C++
Java
Python3
C#
Javascript
// C++ implementation to Print N distinct// numbers such that their sum is 0 #include <bits/stdc++.h>using namespace std; // Function to print distinct n// numbers such that their sum is 0void findNumbers(int N){    for (int i = 1; i <= N / 2; i++) {         // Print 2 symmetric numbers        cout << i << ", " << -i << ", ";    }     // print a extra 0 if N is odd    if (N % 2 == 1)        cout << 0;} // Driver codeint main(){    int N = 10;     findNumbers(N);}
// Java implementation to Print N distinct// numbers such that their sum is 0 class GFG{ // Function to print distinct n// numbers such that their sum is 0static void findNumbers(int N){    for (int i = 1; i <= N / 2; i++)    {        // Print 2 symmetric numbers        System.out.print(i + ", " + -i + ", ");    }     // Print a extra 0 if N is odd    if (N % 2 == 1)        System.out.print(0);} // Driver codepublic static void main(String[] args){    int N = 10;    findNumbers(N);}} // This code is contributed by 29AjayKumar
# Python3 implementation to print N distinct# numbers such that their sum is 0 # Function to print distinct n# numbers such that their sum is 0def findNumbers(N):         for i in range(1, N // 2 + 1):                 # Print 2 symmetric numbers        print(i, end = ', ')        print(-i, end = ', ')             # Print a extra 0 if N is odd    if N % 2 == 1:        print(0, end = '')     # Driver codeif __name__=='__main__':         N = 10    findNumbers(N) # This code is contributed by rutvik_56
// C# implementation to print N distinct// numbers such that their sum is 0using System; class GFG { // Function to print distinct n// numbers such that their sum is 0static void findNumbers(int N){    for(int i = 1; i <= (N / 2); i++)    {        // Print 2 symmetric numbers       Console.Write(i + ", " + -i + ", ");    }         // Print a extra 0 if N is odd    if (N % 2 == 1)        Console.Write(0);} // Driver codestatic void Main(){    int N = 10;         findNumbers(N);}} // This code is contributed by divyeshrabadiya07   
<script> // Javascript implementation to Print N distinct// numbers such that their sum is 0 // Function to print distinct n// numbers such that their sum is 0function findNumbers(N){    for (var i = 1; i <= N / 2; i++) {         // Print 2 symmetric numbers        document.write( i + ", " + -i + ", ");    }     // print a extra 0 if N is odd    if (N % 2 == 1)        document.write( 0);} // Driver codevar N = 10;findNumbers(N); </script>
1, -1, 2, -2, 3, -3, 4, -4, 5, -5,
 
Time Complexity: O(N) 
29AjayKumar
rutvik_56
divyeshrabadiya07
itsok
ardotrtrian
Numbers
Mathematical
School Programming
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Modular multiplicative inverse
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java