Count of ways to write N as a sum of three numbers Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a positive integer N, count number of ways to write N as a sum of three numbers. For numbers which are not expressible print -1.Examples: Input: N = 4 Output: 3 Explanation: ( 1 + 1 + 2 ) = 4 ( 1 + 2 + 1 ) = 4 ( 2 + 1 + 1 ) = 4. So in total, there are 3 ways.Input: N = 5 Output: 6 ( 1 + 1 + 3 ) = 5 ( 1 + 3 + 1 ) = 5 ( 3 + 1 + 1 ) = 5 ( 1 + 2 + 2 ) = 5 ( 2 + 2 + 1 ) = 5 ( 2 + 1 + 2 ) = 5. So in total, there are 6 ways Approach: To solve the problem mentioned above if we take a closer look we will observe a pattern in solution to the question. For all the numbers that are greater than 2 we get a series 3, 6, 10, 15, 25 and so on, which is nothing but the sum of first N-1 natural numbers.Below is the implementation of the above approach: C++ // C++ program to count the total number of // ways to write N as a sum of three numbers #include <bits/stdc++.h> using namespace std; // Function to find the number of ways void countWays(int n) { // Check if number is less than 2 if (n <= 2) cout << "-1"; else { // Calculate the sum int ans = (n - 1) * (n - 2) / 2; cout << ans; } } // Driver code int main() { int N = 5; countWays(N); return 0; } Java // Java program to count the total number of // ways to write N as a sum of three numbers class GFG{ // Function to find the number of ways static void countWays(int n) { // Check if number is less than 2 if (n <= 2) { System.out.print("-1"); } else { // Calculate the sum int ans = (n - 1) * (n - 2) / 2; System.out.print(ans); } } // Driver code public static void main(String[] args) { int N = 5; countWays(N); } } // This code is contributed by Amit Katiyar Python3 # Python3 program to count the total number of # ways to write N as a sum of three numbers def countWays(N): # Check if number is less than 2 if (N <= 2): print("-1") else: # Calculate the sum ans = (N - 1) * (N - 2) / 2 print(ans) # Driver code if __name__ == '__main__': N = 5 countWays(N) # This code is contributed by coder001 C# // C# program to count the total number of // ways to write N as a sum of three numbers using System; class GFG{ // Function to find the number of ways static void countWays(int n) { // Check if number is less than 2 if (n <= 2) { Console.WriteLine("-1"); } else { // Calculate the sum int ans = (n - 1) * (n - 2) / 2; Console.WriteLine(ans); } } // Driver code static void Main() { int N = 5; countWays(N); } } // This code is contributed by divyeshrabadiya07 JavaScript <script> // Javascript program to count the total number of // ways to write N as a sum of three numbers // Function to find the number of ways function countWays(n) { // Check if number is less than 2 if (n <= 2) document.write( "-1"); else { // Calculate the sum var ans = (n - 1) * (n - 2) / 2; document.write( ans); } } // Driver code var N = 5; countWays(N); </script> Output: 6 Time Complexity: O(1) Auxiliary Space: O(1) as constant space for variables is being used Comment More infoAdvertise with us Next Article Count of triplets in an Array with odd sum S spp____ Follow Improve Article Tags : DSA series Natural Numbers Practice Tags : series Similar Reads Count of different ways to express N as the sum of 1, 3 and 4 Given a positive integer n, the task is to count the number of ways to express n as a sum of 1, 3 and 4.Examples: Input: n = 4Output: 4Explanation: There is 4 ways to represent 4 as sum of 1, 3 and 4: (1+1+1+1), (1+3), (3+1) and (4).Input: n = 3Output: 2Explanation: There is 2 ways to represent 3 as 13 min read 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 3 min read Count of triplets in an Array with odd sum Given an array arr[] with N integers, find the number of triplets of i, j and k such that 1<= i < j < k <= N and arr[i] + arr[j] + arr[k] is odd. Example: Input: arr[] = {1, 2, 3, 4, 5}Output: 4Explanation: The given array contains 4 triplets with an odd sum. They are {1, 2, 4}, {1, 3, 5 6 min read Count Triplets such that one of the numbers can be written as sum of the other two Given an array A[] of N integers. The task is to find the number of triples (i, j, k) , where i, j, k are indices and (1 <= i < j < k <= N), such that in the set { A_i , A_j , A_k } at least one of the numbers can be written as the sum of the other two.Examples: Input : A[] = {1, 2, 3, 4 10 min read Equal Sum and XOR of three Numbers Given an integer N. The task is to count the numbers of pairs of integers A and B such that A + B + N = A ^ B ^ N and A and B are less than N.Examples: Input: N = 2 Output: 3 Explanation:- For N = 2 2 XOR 0 XOR 0 = 2+0+0 2 XOR 0 XOR 1 = 2+0+1 2 XOR 0 XOR 2 != 2+0+2 2 XOR 1 XOR 0 = 2+1+0 2 XOR 1 XOR 5 min read Equal Sum and XOR of three Numbers Given an integer N. The task is to count the numbers of pairs of integers A and B such that A + B + N = A ^ B ^ N and A and B are less than N.Examples: Input: N = 2 Output: 3 Explanation:- For N = 2 2 XOR 0 XOR 0 = 2+0+0 2 XOR 0 XOR 1 = 2+0+1 2 XOR 0 XOR 2 != 2+0+2 2 XOR 1 XOR 0 = 2+1+0 2 XOR 1 XOR 5 min read Like