Skip to content
Related Articles

Related Articles

Number of non-negative integral solutions of a + b + c = n

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 24 Jun, 2022
Improve Article
Save Article

Given a number n, find the number of ways in which  we can add 3 non-negative integers so that their sum is n.
Examples : 
 

Input : n = 1
Output : 3
There are three ways to get sum 1.
(1, 0, 0), (0, 1, 0) and (0, 0, 1)

Input : n = 2
Output : 6
There are six ways to get sum 2.
(2, 0, 0), (0, 2, 0), (0, 0, 2), (1, 1, 0)
(1, 0, 1) and (0, 1, 1)

Input : n = 3
Output : 10
There are ten ways to get sum 3.
(3, 0, 0), (0, 3, 0), (0, 0, 3), (1, 2, 0)
(1, 0, 2), (0, 1, 2), (2, 1, 0), (2, 0, 1),
(0, 2, 1) and (1, 1, 1)

 

Method 1 [ Brute Force : O(n3) ] 
A simple solution is to consider all triplets using three loops. For every triplet, check if its sum is equal to n. If the sum is n, increment the count of solutions. 
Below is the implementation. 
 

C++




// A naive C++ solution to count solutions of
// a + b + c = n
#include<bits/stdc++.h>
using namespace std;
 
// Returns count of solutions of a + b + c = n
int countIntegralSolutions(int n)
{
    // Initialize result
    int result = 0;
 
    // Consider all triplets and increment
    // result whenever sum of a triplet is n.
    for (int i=0; i<=n; i++)
      for (int j=0; j<=n-i; j++)
          for (int k=0; k<=(n-i-j); k++)
             if (i + j + k == n)
              result++;
 
    return result;
}
 
// Driver code
int main()
{
    int n = 3;
    cout <<  countIntegralSolutions(n);
    return 0;
}

Java




// A naive Java solution to count
// solutions of a + b + c = n
import java.io.*;
 
class GFG
{
    // Returns count of solutions of a + b + c = n
    static int countIntegralSolutions(int n)
    {
        // Initialize result
        int result = 0;
     
        // Consider all triplets and increment
        // result whenever sum of a triplet is n.
        for (int i = 0; i <= n; i++)
        for (int j = 0; j <= n - i; j++)
            for (int k = 0; k <= (n - i - j); k++)
                if (i + j + k == n)
                result++;
     
        return result;
    }
 
    // Driver code
    public static void main (String[] args)
    {
        int n = 3;
        System.out.println( countIntegralSolutions(n));
     
    }
}
 
// This article is contributed by vt_m

Python3




# Python3 code to count
# solutions of a + b + c = n
 
# Returns count of solutions
# of a + b + c = n
def countIntegralSolutions (n):
 
    # Initialize result
    result = 0
     
    # Consider all triplets and
    # increment result whenever
    # sum of a triplet is n.
    for i in range(n + 1):
        for j in range(n + 1):
            for k in range(n + 1):
                if i + j + k == n:
                    result += 1
     
    return result
     
# Driver code
n = 3
print(countIntegralSolutions(n))
 
# This code is contributed by "Sharad_Bhardwaj".

C#




// A naive C# solution to count
// solutions of a + b + c = n
using System;
 
class GFG {
     
    // Returns count of solutions
    // of a + b + c = n
    static int countIntegralSolutions(int n)
    {
         
        // Initialize result
        int result = 0;
     
        // Consider all triplets and increment
        // result whenever sum of a triplet is n.
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= n - i; j++)
                for (int k = 0; k <= (n - i - j); k++)
                    if (i + j + k == n)
                    result++;
     
        return result;
    }
 
    // Driver code
    public static void Main ()
    {
        int n = 3;
        Console.Write(countIntegralSolutions(n));
     
    }
}
 
// This article is contributed by Smitha.

PHP




<?php
// A naive PHP solution
// to count solutions of
// a + b + c = n
 
// Returns count of
// solutions of a + b + c = n
function countIntegralSolutions( $n)
{
     
    // Initialize result
    $result = 0;
 
    // Consider all triplets and increment
    // result whenever sum of a triplet is n.
    for ($i = 0; $i <= $n; $i++)
        for ($j = 0; $j <= $n - $i; $j++)
            for ($k = 0; $k <= ($n - $i - $j); $k++)
            if ($i + $j + $k == $n)
            $result++;
 
    return $result;
}
 
    // Driver Code
    $n = 3;
    echo countIntegralSolutions($n);
 
// This code is contributed by anuj_67.
?>

Javascript




<script>
 
// Javascript program solution to count
// solutions of a + b + c = n
 
  // Returns count of solutions of a + b + c = n
    function countIntegralSolutions(n)
    {
        // Initialize result
        let result = 0;
       
        // Consider all triplets and increment
        // result whenever sum of a triplet is n.
        for (let i = 0; i <= n; i++)
        for (let j = 0; j <= n - i; j++)
            for (let k = 0; k <= (n - i - j); k++)
                if (i + j + k == n)
                result++;
       
        return result;
    }
 
// Driver Code
 
    let n = 3;
    document.write(countIntegralSolutions(n));
 
</script>

Output: 

10

Time complexity: O(n3)

Auxiliary Space: O(1)

Method 2 [ Direct Formula : O(1) ] 
If we take a closer look at the pattern, we can find that the count of solutions is ((n+1) * (n+2)) / 2. The problem is equivalent to distributing n identical balls  in three boxes and the solution is n+2C2. In general, if there are m variables (or boxes) and n balls , the formula becomes n+m-1Cm-1
 

C++




// A naive C++ solution to count solutions of
// a + b + c = n
#include<bits/stdc++.h>
using namespace std;
 
// Returns count of solutions of a + b + c = n
int countIntegralSolutions(int n)
{
    return ((n+1)*(n+2))/2;
}
 
// Driver code
int main()
{
    int n = 3;
    cout <<  countIntegralSolutions(n);
    return 0;
}

Java




// Java solution to count
// solutions of a + b + c = n
import java.io.*;
 
class GFG
{
    // Returns count of solutions
    // of a + b + c = n
    static int countIntegralSolutions(int n)
    {
    return ((n + 1) * (n + 2)) / 2;
         
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 3;
        System.out.println ( countIntegralSolutions(n));
         
    }
}
// This article is contributed by vt_m

Python3




# Python3 solution to count
# solutions of a + b + c = n
 
# Returns count of solutions
# of a + b + c = n
def countIntegralSolutions (n):
    return int(((n + 1) * (n + 2)) / 2)
     
# Driver code
n = 3
print(countIntegralSolutions(n))
 
# This code is contributed by "Sharad_Bhardwaj".

C#




// C# solution to count
// solutions of a + b + c = n
using System;
 
class GFG {
     
    // Returns count of solutions
    // of a + b + c = n
    static int countIntegralSolutions(int n)
    {
        return ((n + 1) * (n + 2)) / 2;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int n = 3;
        Console.Write ( countIntegralSolutions(n));
         
    }
}
 
// This code is contributed by parashar.

PHP




<?php
// A naive PHP solution
// to count solutions of
// a + b + c = n
 
// Returns count of solutions
// of a + b + c = n
function countIntegralSolutions($n)
{
    return (($n + 1) * ($n + 2)) / 2;
}
 
    // Driver Code
    $n = 3;
    echo countIntegralSolutions($n);
 
// This code is contributed by anuj_67.
?>

Javascript




<script>
 
// A naive JavaScript solution
// to count solutions of
// a + b + c = n
 
// Returns count of solutions of a + b + c = n
function countIntegralSolutions(n)
{
    return Math.floor(((n+1)*(n+2))/2);
}
 
// Driver code
 
    let n = 3;
    document.write(countIntegralSolutions(n));
 
 
// This code is contributed by Surbhi Tyagi.
 
</script>

Output : 

10

Time complexity: O(1)

Auxiliary Space: O(1)
This article is contributed by Shivam Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!