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++

`// 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 0 ` `void` `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 code ` `int` `main() ` `{ ` ` ` `int` `N = 10; ` ` ` ` ` `findNumbers(N); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// 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 0 ` `static` `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 code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `10` `; ` ` ` `findNumbers(N); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to print N distinct ` `# numbers such that their sum is 0 ` ` ` `# Function to print distinct n ` `# numbers such that their sum is 0 ` `def` `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 code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `10` ` ` `findNumbers(N) ` ` ` `# This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to print N distinct ` `// numbers such that their sum is 0 ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Function to print distinct n ` `// numbers such that their sum is 0 ` `static` `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 code ` `static` `void` `Main() ` `{ ` ` ` `int` `N = 10; ` ` ` ` ` `findNumbers(N); ` `} ` `} ` ` ` `// This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

**Output:**

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.

## Recommended Posts:

- Find N distinct integers with sum N
- 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 the prime P using given four integers
- Find K consecutive integers such that their sum is N
- Find N integers with given difference between product and sum
- Find sum in range L to R in given sequence of 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 the number of integers from 1 to n which contains digits 0's and 1's only
- Find three integers less than or equal to N such that their LCM is maximum
- Find Prime Adam integers in the given range [L, R]

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.