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; } |
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 |
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 |
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 |
1
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:
- Check whether a number can be represented as sum of K distinct positive integers
- Find the smallest positive number which can not be represented by given digits
- Count of ways in which N can be represented as sum of Fibonacci numbers without repetition
- Check if N can be represented as sum of squares of two consecutive integers
- Ways to write N as sum of two or more positive integers | Set-2
- Print all possible ways to write N as sum of two or more positive integers
- Check if N can be represented as sum of integers chosen from set {A, B}
- Count numbers which can be represented as sum of same parity primes
- Check if given number can be represented as sum of two great numbers
- Check whether a number can be represented by sum of two squares
- Check if a number can be represented as sum of two consecutive perfect cubes
- Maximum number of distinct positive integers that can be used to represent N
- Count array elements that can be represented as sum of at least two consecutive array elements
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Check whether a number can be represented as difference of two squares
- Check whether a number can be represented by the product of two squares
- Check whether a number can be represented as difference of two consecutive cubes
- Number of arrays of size N whose elements are positive integers and sum is K
- Check if a number can be represented as a sum of 2 triangular numbers
- Check if a number can be represented as sum of non zero powers of 2
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.

