Given a positive integer **N**. The task is to find the number of ways in which you can express N as a sum of exactly two numbers **A** and **B** (N = A + B) where A > 0, B > 0 and B > A.

**Examples:**

Input:N = 8Output:3Explanation:N = 8 can be expressed as (1, 7), (2, 6), (3, 5)Input:N = 14Output:6

**Approach:**

- An observation here is that for every number N if we take a number A which is less than N/2 then, there must be a number B which is greater than N/2 and A + B = N.
- This leads to a simple solution of finding the count of numbers for either B or A. Hence the floor value of (N-1)/2 will lead to the solution.

## C++

`// C++ program to Count ways to ` `// express a number as sum of ` `// two numbers. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function returns the count ` `// of ways express a number ` `// as sum of two numbers. ` `int` `CountWays(` `int` `n) ` `{ ` ` ` `int` `ans = (n - 1) / 2; ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 8; ` ` ` ` ` `cout << CountWays(N); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count ways to ` `// express a number as sum of ` `// two numbers. ` `class` `GFG{ ` ` ` `// Function returns the count ` `// of ways express a number ` `// as sum of two numbers. ` `static` `int` `CountWays(` `int` `n) ` `{ ` ` ` `int` `ans = (n - ` `1` `) / ` `2` `; ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `8` `; ` ` ` `System.out.print(CountWays(N)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to Count ways to ` `# express a number as sum of ` `# two numbers. ` ` ` `# Function returns the count ` `# of ways express a number ` `# as sum of two numbers. ` `def` `CountWays(n) : ` ` ` `ans ` `=` `(n ` `-` `1` `) ` `/` `/` `2` ` ` `return` `ans ` ` ` `# Driver code ` `N ` `=` `8` `print` `(CountWays(N)) ` ` ` `# This code is contributed by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count ways to ` `// express a number as sum of ` `// two numbers. ` `using` `System; ` `class` `GFG{ ` ` ` `// Function returns the count ` `// of ways express a number ` `// as sum of two numbers. ` `static` `int` `CountWays(` `int` `n) ` `{ ` ` ` `int` `ans = (n - 1) / 2; ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 8; ` ` ` `Console.Write(CountWays(N)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time complexity:** O(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:

- Count ways to express a number as sum of consecutive numbers
- Count ways to express even number ānā as sum of even integers
- Ways to express a number as product of two different factors
- Number of ways to select exactly K even numbers from given Array
- Count ways to distribute exactly one coin to each worker
- Numbers less than N which are product of exactly two distinct prime numbers
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Number of ways to erase exactly one element in the Binary Array to make XOR zero
- Express a number as sum of consecutive numbers
- Express an odd number as sum of prime numbers
- Count number of ways to divide an array into two halves with same sum
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Minimum numbers needed to express every integer below N as a sum
- Count of ways to write N as a sum of three numbers
- Count of ways in which N can be represented as sum of Fibonacci numbers without repetition
- Count of Numbers in a Range where digit d occurs exactly K times
- Count of subarrays having exactly K prime numbers
- Count of subarrays having exactly K perfect square numbers
- Count of subsequences which consists exactly K prime numbers

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.