# Number of ways in which N can be represented as the sum of two positive integers

Given a number **N**, the task is to find the number of unique ways in which **N** can be represented as a sum of two positive integers.

**Examples:**

Input:N = 7

Output:3

(1 + 6), (2 + 5) and (3 + 4).

Input:N = 200

Output:100

**Approach:** The number of ways in which the number can be expressed as the sum of two positive integers are **1 + (N – 1), 2 + (N – 2), …, (N – 1) + 1** and **(N – 2) + 2**. There are **N – 1** terms in the series and they appear in identical pairs i.e. **(X + Y, Y + X)**. So the required count will be **N / 2**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number of ` `// distinct ways to represent n ` `// as the sum of two integers ` `int` `ways(` `int` `n) ` `{ ` ` ` `return` `n / 2; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 2; ` ` ` ` ` `cout << ways(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the number of ` ` ` `// distinct ways to represent n ` ` ` `// as the sum of two integers ` ` ` `static` `int` `ways(` `int` `n) ` ` ` `{ ` ` ` `return` `n / ` `2` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `2` `; ` ` ` ` ` `System.out.println(ways(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the number of ` `# distinct ways to represent n ` `# as the sum of two integers ` `def` `ways(n): ` ` ` `return` `n ` `/` `/` `2` ` ` `# Driver code ` `n ` `=` `2` ` ` `print` `(ways(n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the number of ` `// distinct ways to represent n ` `// as the sum of two integers ` `static` `int` `ways(` `int` `n) ` `{ ` ` ` `return` `n / 2; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 2; ` ` ` ` ` `Console.WriteLine(ways(n)); ` `} ` `} ` ` ` `// This code is contributed by Nidhi_Biet ` |

*chevron_right*

*filter_none*

**Output:**

1

## Recommended Posts:

- Write a program to reverse digits of a number
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Minimum number of jumps to reach end
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Print all combinations of points that can compose a given number
- Check if a number is multiple of 5 without using / and % operators
- Median in a stream of integers (running integers)
- Given a number, find the next smallest palindrome
- Count the number of possible triangles
- Select a random number from stream, with O(1) space
- Program to convert a given number to words
- Efficient program to print all prime factors of a given number

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.