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.

**Examples:**

Input:N = 3Output:1, -1, 3Explanation:

On adding the numbers that is 1 + (-1) + 3 the sum is 3.

Input:N = 4Output:1, -1, 0, 4Explanation:

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:

## C++

`// C++ for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to print distinct N` `// numbers whose sum is N` `void` `findNumbers(` `int` `N)` `{` ` ` `// To store how many symmetric` ` ` `// pairs needs to be calculated` ` ` `int` `half = N / 2;` ` ` `// For even N we have to print` ` ` `// one less symmetric pair` ` ` `if` `(N % 2 == 0) {` ` ` `half--;` ` ` `}` ` ` `// Iterate till [1 n/2] and Print` ` ` `// all symmetric pairs(i, -i)` ` ` `for` `(` `int` `i = 1; i <= half; i++) {` ` ` `// Print 2 symmetric numbers` ` ` `cout << (-1) * i` ` ` `<< ` `", "` `<< i << ` `", "` `;` ` ` `}` ` ` `// if N is Odd, then print N` ` ` `if` `(N & 1) {` ` ` `cout << N << endl;` ` ` `}` ` ` `// Else print(0, N)` ` ` `else` `{` ` ` `cout << 0 << ` `", "` ` ` `<< N << endl;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Sum` ` ` `int` `N = 5;` ` ` `// Function Call` ` ` `findNumbers(N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java for the above approach` `class` `GFG{` ` ` `// Function to print distinct N ` `// numbers whose sum is N ` `public` `static` `void` `findNumbers(` `int` `N) ` `{ ` ` ` ` ` `// To store how many symmetric ` ` ` `// pairs needs to be calculated ` ` ` `int` `half = N / ` `2` `; ` ` ` ` ` `// For even N we have to print ` ` ` `// one less symmetric pair ` ` ` `if` `(N % ` `2` `== ` `0` `)` ` ` `{ ` ` ` `half--; ` ` ` `} ` ` ` ` ` `// Iterate till [1 n/2] and Print ` ` ` `// all symmetric pairs(i, -i) ` ` ` `for` `(` `int` `i = ` `1` `; i <= half; i++)` ` ` `{ ` ` ` `// Print 2 symmetric numbers ` ` ` `System.out.print((-` `1` `) * i + ` `", "` `+` ` ` `i + ` `", "` `);` ` ` `} ` ` ` ` ` `// if N is Odd, then print N ` ` ` `int` `check = N & ` `1` `;` ` ` `if` `(check != ` `0` `)` ` ` `{ ` ` ` `System.out.println(N);` ` ` `} ` ` ` ` ` `// Else print(0, N)` ` ` `else` ` ` `{` ` ` `System.out.println(` `0` `+ ` `", "` `+ N);` ` ` `}` `} ` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Given sum ` ` ` `int` `N = ` `5` `; ` ` ` ` ` `// Function sall ` ` ` `findNumbers(N); ` `}` `}` `// This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code for the above approach ` `# Function to print distinct N ` `# numbers whose sum is N ` `def` `findNumbers(N):` ` ` `# To store how many symmetric ` ` ` `# pairs needs to be calculated ` ` ` `half ` `=` `int` `(N ` `/` `2` `) ` ` ` `# For even N we have to print ` ` ` `# one less symmetric pair ` ` ` `if` `(N ` `%` `2` `=` `=` `0` `): ` ` ` `half ` `=` `half ` `-` `1` ` ` `# Iterate till [1 n/2] and Print ` ` ` `# all symmetric pairs(i, -i) ` ` ` `for` `i ` `in` `range` `(` `1` `, half ` `+` `1` `):` ` ` `# Print 2 symmetric numbers ` ` ` `print` `((` `-` `1` `) ` `*` `i, end ` `=` `', '` `)` ` ` `print` `(i, end ` `=` `', '` `)` ` ` `# If N is Odd, then print N ` ` ` `if` `(N & ` `1` `): ` ` ` `print` `(N, end ` `=` `'\n'` `)` ` ` `# Else print(0, N)` ` ` `else` `:` ` ` `print` `(` `0` `, end ` `=` `', '` `)` ` ` `print` `(N, end ` `=` `'\n'` `) ` `# Driver Code ` `N ` `=` `5` `# Function Call ` `findNumbers(N)` `# This code is contributed by PratikBasu ` |

*chevron_right*

*filter_none*

## C#

`// C# for the above approach` `using` `System;` `class` `GFG{` ` ` `// Function to print distinct N ` `// numbers whose sum is N ` `public` `static` `void` `findNumbers(` `int` `N) ` `{ ` ` ` ` ` `// To store how many symmetric ` ` ` `// pairs needs to be calculated ` ` ` `int` `half = N / 2; ` ` ` ` ` `// For even N we have to print ` ` ` `// one less symmetric pair ` ` ` `if` `(N % 2 == 0)` ` ` `{ ` ` ` `half--; ` ` ` `} ` ` ` ` ` `// Iterate till [1 n/2] and Print ` ` ` `// all symmetric pairs(i, -i) ` ` ` `for` `(` `int` `i = 1; i <= half; i++)` ` ` `{ ` ` ` `// Print 2 symmetric numbers ` ` ` `Console.Write((-1) * i + ` `", "` `+` ` ` `i + ` `", "` `);` ` ` `} ` ` ` ` ` `// if N is Odd, then print N ` ` ` `int` `check = N & 1;` ` ` `if` `(check != 0)` ` ` `{ ` ` ` `Console.Write(N + ` `"\n"` `);` ` ` `} ` ` ` ` ` `// Else print(0, N)` ` ` `else` ` ` `{` ` ` `Console.Write(0 + ` `", "` `+ N + ` `"\n"` `);` ` ` `}` `} ` `// Driver code` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` ` ` `// Given sum ` ` ` `int` `N = 5; ` ` ` ` ` `// Function sall ` ` ` `findNumbers(N); ` `}` `}` `// This code is contributed by rutvik_56` |

*chevron_right*

*filter_none*

**Output:**

-1,1,-2,2,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.

## Recommended Posts:

- 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 zero sum
- 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)
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding 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
- Find N integers with given difference between product and sum
- Find the first N integers such that the sum of their digits is equal to 10
- Find sum in range L to R in given sequence of integers
- Find K consecutive integers such that their sum is N
- Sum of M maximum distinct digit sum from 1 to N that are factors of K
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- How to sum two integers without using arithmetic operators in C/C++?

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.