Tuesday, November 18, 2025
HomeData Modelling & AICount ways to express a number as sum of exactly two numbers

Count ways to express a number as sum of exactly two numbers

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 = 8
Output: 3
Explanation:
N = 8 can be expressed as (1, 7), (2, 6), (3, 5)

Input: N = 14
Output: 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);
}


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


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


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


Javascript




<script>
 
// Javascript 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.
function CountWays(n)
{
    let ans = Math.floor((n - 1) / 2);
       
    return ans;
}
 
// Driver Code
     
    let N = 8;
    document.write(CountWays(N));
         
</script>


Output: 

3

 

Time complexity: O(1)

Auxiliary space: O(1) because it is using constant space for variable

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32402 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6772 POSTS0 COMMENTS
Nicole Veronica
11922 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11992 POSTS0 COMMENTS
Shaida Kate Naidoo
6899 POSTS0 COMMENTS
Ted Musemwa
7156 POSTS0 COMMENTS
Thapelo Manthata
6853 POSTS0 COMMENTS
Umr Jansen
6845 POSTS0 COMMENTS