Count number of ways to divide a number in 4 parts
Last Updated :
21 Jul, 2022
Given a positive integer n, find number of ways to divide n in four parts or represent n as sum of four positive integers. Here n varies from 0 to 5000.
Examples :
Input: n = 5
Output: 1
There is only one way (1, 1, 1, 2)
Input: n = 6
Output: 2
There are two ways (1, 1, 1, 3) and (1, 1, 2, 2)
Input: n = 8
Output: 5
There are five ways (2, 2, 2, 2), (1, 1, 1, 5),(1, 1, 3, 3), (1, 1, 2, 4) and (1, 2, 2, 3)
Method 1 (Simple Solution) Run four nested loops to generate all possible different quadruplet. Below is C++ implementation of the simple algorithm.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int countWays( int n)
{
int counter = 0;
for ( int i = 1; i < n; i++)
for ( int j = i; j < n; j++)
for ( int k = j; k < n; k++)
for ( int l = k; l < n; l++)
if (i + j + k + l == n)
counter++;
return counter;
}
int main()
{
int n = 8;
cout << countWays(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countWays( int n)
{
int counter = 0 ;
for ( int i = 1 ; i < n; i++)
for ( int j = i; j < n; j++)
for ( int k = j; k < n; k++)
for ( int l = k; l < n; l++)
if (i + j + k + l == n)
counter++;
return counter;
}
public static void main (String[] args) {
int n = 8 ;
System.out.println (countWays(n));
}
}
|
Python3
def countWays(n):
counter = 0
for i in range ( 1 , n):
for j in range (i, n):
for k in range (j, n):
for l in range (k, n):
if (i + j + k + l = = n):
counter + = 1
return counter
if __name__ = = "__main__" :
n = 8
print (countWays(n))
|
C#
using System;
class GFG
{
static int countWays( int n)
{
int counter = 0;
for ( int i = 1; i < n; i++)
for ( int j = i; j < n; j++)
for ( int k = j; k < n; k++)
for ( int l = k; l < n; l++)
if (i + j + k + l == n)
counter++;
return counter;
}
static public void Main ()
{
int n = 8;
Console.WriteLine(countWays(n));
}
}
|
PHP
<?php
function countWays( $n )
{
$counter = 0;
for ( $i = 1; $i < $n ; $i ++)
for ( $j = $i ; $j < $n ; $j ++)
for ( $k = $j ; $k < $n ; $k ++)
for ( $l = $k ; $l < $n ; $l ++)
if ( $i + $j + $k + $l == $n )
$counter ++;
return $counter ;
}
$n = 8;
echo countWays( $n );
?>
|
Javascript
<script>
function countWays(n)
{
let counter = 0;
for (let i = 1; i < n; i++)
for (let j = i; j < n; j++)
for (let k = j; k < n; k++)
for (let l = k; l < n; l++)
if (i + j + k + l == n)
counter++;
return counter;
}
let n = 8;
document.write(countWays(n));
</script>
|
Time complexity of above solution is O(n4)
Auxiliary Space: O(1)
Method 2 (Uses Dynamic Programming)
The idea is based on below recursive solution.
countWays(n, parts, nextPart) = ?countWays(n, parts, i)
nextPart <= i Input number
parts --> Count of parts of n. Initially parts = 4
nextPart --> Starting point for next part to be tried
We try for all values from nextPart to n.
We initially call the function as countWays(n, 4, 1)
Below is Dynamic Programming based on solution of above idea.
C++
#include<bits/stdc++.h>
using namespace std;
int dp[5001][5001][5];
int countWaysUtil( int n, int parts, int nextPart)
{
if (parts == 0 && n == 0) return 1;
if (n <= 0 || parts <= 0) return 0;
if (dp[n][nextPart][parts] != -1)
return dp[n][nextPart][parts];
int ans = 0;
for ( int i = nextPart; i <= n; i++)
ans += countWaysUtil(n-i, parts-1, i);
return (dp[n][nextPart][parts] = ans);
}
int countWays( int n)
{
memset (dp, -1, sizeof (dp));
return countWaysUtil(n, 4, 1);
}
int main()
{
int n = 8;
cout << countWays(n) << endl;
return 0;
}
|
Java
class GFG
{
static int dp[][][] = new int [ 5001 ][ 5001 ][ 5 ];
static int countWaysUtil( int n, int parts, int nextPart)
{
if (parts == 0 && n == 0 ) return 1 ;
if (n <= 0 || parts <= 0 ) return 0 ;
if (dp[n][nextPart][parts] != - 1 )
return dp[n][nextPart][parts];
int ans = 0 ;
for ( int i = nextPart; i <= n; i++)
ans += countWaysUtil(n-i, parts- 1 , i);
return (dp[n][nextPart][parts] = ans);
}
static int countWays( int n)
{
for ( int i = 0 ; i < 5001 ; i++)
{
for ( int j = 0 ; j < 5001 ; j++)
{
for ( int l = 0 ; l < 5 ; l++)
dp[i][j][l] = - 1 ;
}
}
return countWaysUtil(n, 4 , 1 );
}
public static void main(String[] args)
{
int n = 8 ;
System.out.println(countWays(n));
}
}
|
Python3
dp = [[[ - 1 for i in range ( 5 )]
for i in range ( 501 )]
for i in range ( 501 )]
def countWaysUtil(n, parts, nextPart):
if (parts = = 0 and n = = 0 ):
return 1
if (n < = 0 or parts < = 0 ):
return 0
if (dp[n][nextPart][parts] ! = - 1 ):
return dp[n][nextPart][parts]
ans = 0
for i in range (nextPart, n + 1 ):
ans + = countWaysUtil(n - i, parts - 1 , i)
dp[n][nextPart][parts] = ans
return (ans)
def countWays(n):
return countWaysUtil(n, 4 , 1 )
n = 8
print (countWays(n))
|
C#
using System;
class GFG
{
static int [,,]dp = new int [5001, 5001, 5];
static int countWaysUtil( int n, int parts, int nextPart)
{
if (parts == 0 && n == 0) return 1;
if (n <= 0 || parts <= 0) return 0;
if (dp[n,nextPart,parts] != -1)
return dp[n,nextPart,parts];
int ans = 0;
for ( int i = nextPart; i <= n; i++)
ans += countWaysUtil(n - i, parts - 1, i);
return (dp[n,nextPart,parts] = ans);
}
static int countWays( int n)
{
for ( int i = 0; i < 5001; i++)
{
for ( int j = 0; j < 5001; j++)
{
for ( int l = 0; l < 5; l++)
dp[i, j, l] = -1;
}
}
return countWaysUtil(n, 4, 1);
}
public static void Main(String[] args)
{
int n = 8;
Console.WriteLine(countWays(n));
}
}
|
PHP
<?php
$dp = array_fill (0, 501,
array_fill (0, 501,
array_fill (0, 5, -1)));
function countWaysUtil( $n , $parts , $nextPart )
{
global $dp ;
if ( $parts == 0 && $n == 0) return 1;
if ( $n <= 0 || $parts <= 0) return 0;
if ( $dp [ $n ][ $nextPart ][ $parts ] != -1)
return $dp [ $n ][ $nextPart ][ $parts ];
$ans = 0;
for ( $i = $nextPart ; $i <= $n ; $i ++)
$ans += countWaysUtil( $n - $i ,
$parts - 1, $i );
return ( $dp [ $n ][ $nextPart ][ $parts ] = $ans );
}
function countWays( $n )
{
return countWaysUtil( $n , 4, 1);
}
$n = 8;
echo countWays( $n );
?>
|
Javascript
<script>
let dp = new Array(5001);
for (let i = 0; i < 5001; i++)
{
dp[i] = new Array(5001);
for (let j = 0; j < 5001; j++)
{
dp[i][j] = new Array(5);
}
}
function countWaysUtil(n,parts,nextPart)
{
if (parts == 0 && n == 0)
return 1;
if (n <= 0 || parts <= 0)
return 0;
if (dp[n][nextPart][parts] != -1)
return dp[n][nextPart][parts];
let ans = 0;
for (let i = nextPart; i <= n; i++)
ans += countWaysUtil(n - i, parts - 1, i);
return (dp[n][nextPart][parts] = ans);
}
function countWays(n)
{
for (let i = 0; i < 5001; i++)
{
for (let j = 0; j < 5001; j++)
{
for (let l = 0; l < 5; l++)
dp[i][j][l] = -1;
}
}
return countWaysUtil(n, 4, 1);
}
let n = 8;
document.write(countWays(n));
</script>
|
Output :
5
Time Complexity: O(n3). There are ?(n2) entries, every entry is filled only once and filling an entry takes O(n) time.
Auxiliary Space: O(n2)
Method 3 (A O(n2 Log n) Solution)
We can use the solution discussed in this post to find all quadruplets.
Thanks to Gaurav Ahirwar for suggesting above solutions.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...