Open In App

Count of ways to split a given number

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to find the count of unique ways to split it into different parts.

Note: {2, 1} and {1, 2} will be considered as one way.

Examples:

Input: n = 5
Output: 7
Explanation: There are 7 ways to split 5:
5
4 + 1
3 + 2
3 + 1 + 1
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1

Input: n = 3
Output: 3
Explanation: There are 3 ways to split 3:
3
2 + 1
1 + 1 + 1

Count of ways to split a given number using Recursion:

Recurrence Relation:

findWays(remainingSum, currentNumber) = findWays(remainingSum – currentNumber, currentNumber) + findWays(remainingSum, currentNumber + 1)

Approach: To solve the problem follow the below idea:

To count all the ways to split a given number remainingSum into smaller numbers, then for each smaller number currentNumber we have two choices:

  • Split remainingSum by subtracting currentNumber: Subtract the current number X from N and call the findWays function recursively with the updated remainingSum and the same currentNumber i.e., findWays(remainingSum – currentNumber, currentNumber)
  • Exclude the currentNumber: Call the findWays function recursively with the same remainingSum and the greater currentNumber. i.e., findWays(remainingSum, currentNumber + 1).

Step-by-step algorithm:

  • Maintain a recursive function, say findWays(remainingSum, currentNumber) to find the number of ways to split remainingSum into parts of size >= currentNumber.
  • In each recursuve call, there are two choices:
    • Do not break the remainingSum in a part of size = currentNumber and choose to break in a bigger part by calling findWays(remainingSum, currentNumber + 1)
    • Break the remainingSum in a part of size = currentNumber and again call findWays(remainingSum – currentNumber, currentNumber)
  • If at any recursive call, the remainingSum becomes negative then it means that the number cannot split any further, so return 0.
  • If at any recursive call, the remainingSum becomes 0, then it means that the number has already been split, so return 1.
  • After all the recursive calls, return the final answer.

Below is the implementation of the algorithm:

C++
#include <iostream>
using namespace std;

// recursive function to find the number of ways to split a
// number
int findWays(int remainingSum, int currentNumber)
{
    if (remainingSum == 0)
        return 1;
    if (remainingSum < 0 || currentNumber > remainingSum)
        return 0;
    // Skip the current number
    int ways1 = findWays(remainingSum, currentNumber + 1);
    // Choose the current number
    int ways2 = findWays(remainingSum - currentNumber,
                         currentNumber);
    return ways1 + ways2;
}

// function to find the number of ways to split a number
int solve(int n)
{
    if (n <= 0)
        return 0;
    return findWays(n, 1);
}

int main()
{
      // function call
      int n = 5;
    cout << solve(n) << "\n";
    return 0;
}
Java
import java.util.Scanner;

public class Main {
    // Recursive function to find the number of ways to split a number
    static int findWays(int remainingSum, int currentNumber) {
        if (remainingSum == 0)
            return 1;
        if (remainingSum < 0 || currentNumber > remainingSum)
            return 0;
        // Skip the current number
        int ways1 = findWays(remainingSum, currentNumber + 1);
        // Choose the current number
        int ways2 = findWays(remainingSum - currentNumber, currentNumber);
        return ways1 + ways2;
    }

    // Function to find the number of ways to split a number
    static int solve(int n) {
        if (n <= 0)
            return 0;
        return findWays(n, 1);
    }

    public static void main(String[] args) {
        // Input number
        int n = 5;
        // Function call and output result
        System.out.println(solve(n));
    }
}
Python
# Recursive function to find the number of ways to split a number
def find_ways(remaining_sum, current_number):
    if remaining_sum == 0:
        return 1
    if remaining_sum < 0 or current_number > remaining_sum:
        return 0
    # Skip the current number
    ways1 = find_ways(remaining_sum, current_number + 1)
    # Choose the current number
    ways2 = find_ways(remaining_sum - current_number, current_number)
    return ways1 + ways2

# Function to find the number of ways to split a number
def solve(n):
    if n <= 0:
        return 0
    return find_ways(n, 1)

# Main function
def main():
    n = 5  # Define the input value
    # Function call and output
    print(solve(n))

# Call the main function to execute the program
main()
JavaScript
// Recursive function to find the number of ways to split a number
function findWays(remainingSum, currentNumber) {
    if (remainingSum === 0)
        return 1;
    if (remainingSum < 0 || currentNumber > remainingSum)
        return 0;
    // Skip the current number
    const ways1 = findWays(remainingSum, currentNumber + 1);
    // Choose the current number
    const ways2 = findWays(remainingSum - currentNumber, currentNumber);
    return ways1 + ways2;
}

// Function to find the number of ways to split a number
function solve(n) {
    if (n <= 0)
        return 0;
    return findWays(n, 1);
}

// Main function
function main() {
    const n = 5; // Define the input value
    // Function call and output
    console.log(solve(n));
}

// Call the main function to execute the program
main();

Output
7

Time Complexity: O(2^n)
Auxiliary Space: O(n)

Count of ways to split a given number using Dynamic Programming (Memoization):

Algorithm: To solve the problem, follow the below idea:

We can observe that the above recursive approach has Overlapping Subproblems and Optimal Substructure so we can optimize it by storing the already computed results using Memoization. So, now we can use a dp[][] array of size N X N, such that dp[i][j] stores the number of ways to split number i into parts such that each part >= j.

Below is the implementation of the algorithm:

C++
#include <bits/stdc++.h>
using namespace std;

// recursive function to find the number of ways to split a
// number
int findWays(int remainingSum, int currentNumber,
             vector<vector<int> >& dp)
{
    if (remainingSum == 0)
        return 1;
    if (remainingSum < 0 || currentNumber > remainingSum)
        return 0;
    if (dp[remainingSum][currentNumber] != -1)
        return dp[remainingSum][currentNumber];
    // Skip the current number
    int ways1
        = findWays(remainingSum, currentNumber + 1, dp);
    // Choose the current number
    int ways2 = findWays(remainingSum - currentNumber,
                         currentNumber, dp);
    return dp[remainingSum][currentNumber] = ways1 + ways2;
}

// function to find the number of ways to split a number
int solve(int n)
{
    if (n <= 0)
        return 0;
    vector<vector<int> > dp(n + 1, vector<int>(n + 1, -1));
    return findWays(n, 1, dp);
}

int main()
{
    // function call
    cout << solve(5) << "\n";
    return 0;
}
Java
import java.util.*;

public class Main {
    // Recursive function to find the number of ways to
    // split a number
    static int findWays(int remainingSum, int currentNumber,
                        int[][] dp)
    {
        if (remainingSum == 0)
            return 1;
        if (remainingSum < 0
            || currentNumber > remainingSum)
            return 0;
        if (dp[remainingSum][currentNumber] != -1)
            return dp[remainingSum][currentNumber];
        // Skip the current number
        int ways1
            = findWays(remainingSum, currentNumber + 1, dp);
        // Choose the current number
        int ways2 = findWays(remainingSum - currentNumber,
                             currentNumber, dp);
        return dp[remainingSum][currentNumber]
            = ways1 + ways2;
    }

    // Function to find the number of ways to split a number
    static int solve(int n)
    {
        if (n <= 0)
            return 0;
        int[][] dp = new int[n + 1][n + 1];
        for (int[] row : dp) {
            Arrays.fill(row, -1);
        }
        return findWays(n, 1, dp);
    }

    public static void main(String[] args)
    {
        // Function call
        System.out.println(solve(5));
    }
}

// This code is contributed by shivamgupta310570
Python
# Recursive function to find the number of ways to
# split a number


def findWays(remainingSum, currentNumber, dp):
    if remainingSum == 0:
        return 1
    if remainingSum < 0 or currentNumber > remainingSum:
        return 0
    if dp[remainingSum][currentNumber] != -1:
        return dp[remainingSum][currentNumber]
    # Skip the current number
    ways1 = findWays(remainingSum, currentNumber + 1, dp)
    # Choose the current number
    ways2 = findWays(remainingSum - currentNumber, currentNumber, dp)
    dp[remainingSum][currentNumber] = ways1 + ways2
    return dp[remainingSum][currentNumber]

# Function to find the number of ways to split a number


def solve(n):
    if n <= 0:
        return 0
    dp = [[-1] * (n + 1) for _ in range(n + 1)]
    return findWays(n, 1, dp)


# Example usage
print(solve(5))
JavaScript
// Recursive function to find the number of ways to split a number
function findWays(remainingSum, currentNumber, dp) {
    if (remainingSum === 0)
        return 1;
    if (remainingSum < 0 || currentNumber > remainingSum)
        return 0;
    if (dp[remainingSum][currentNumber] !== -1)
        return dp[remainingSum][currentNumber];
    // Skip the current number
    let ways1 = findWays(remainingSum, currentNumber + 1, dp);
    // Choose the current number
    let ways2 = findWays(remainingSum - currentNumber, currentNumber, dp);
    return dp[remainingSum][currentNumber] = ways1 + ways2;
}

// Function to find the number of ways to split a number
function solve(n) {
    if (n <= 0)
        return 0;
    let dp = Array.from({ length: n + 1 }, () => Array(n + 1).fill(-1));
    return findWays(n, 1, dp);
}

// Main function
function main() {
    // Function call
    console.log(solve(5));
}

// Execute the main function
main();

Output
7

Time Complexity: O(n^2)
Auxiliary Space: O(n)



Similar Reads

Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts
Given an array arr[] consisting of N positive integers, the task is to find the minimum possible length of a rod that can be cut into N equal parts such that every ith part can be cut into arr[i] equal parts. Examples: Input: arr[] = {1, 2}Output: 4Explanation:Consider the length of the rod as 4. Then it can be divided in 2 equal parts, each having
7 min read
Count ways to split a Binary String into three substrings having equal count of zeros
Given binary string str, the task is to count the total number of ways to split the given string into three non-overlapping substrings having the same number of 0s. Examples: Input: str = "01010" Output: 4 Explanation: The possible splits are: [0, 10, 10], [01, 01, 0], [01, 0, 10], [0, 101, 0] Input: str = "11111" Output: 0 Approach: To solve the p
16 min read
Count of ways to split given string into two non-empty palindromes
Given a string S, the task is to find the number of ways to split the given string S into two non-empty palindromic strings.Examples: Input: S = "aaaaa" Output: 4 Explanation: Possible Splits: {"a", "aaaa"}, {"aa", "aaa"}, {"aaa", "aa"}, {"aaaa", "a"}Input: S = "abacc" Output: 1 Explanation: Only possible split is "aba", "cc". Recommended: Please t
21 min read
Number of ways to split N as sum of K numbers from the given range
Given four positive integer N, K, L, and R, the task is to split N as sum of K numbers lying in the range [L, R].Note: Since the number of ways can be very large. Output answer modulo 1000000007.Examples: Input : N = 12, K = 3, L = 1, R = 5 Output : 10 {2, 5, 5} {3, 4, 5} {3, 5, 4} {4, 3, 5} {4, 4, 4} {4, 5, 3} {5, 2, 5} {5, 3, 4} {5, 4, 3} {5, 5,
18 min read
Number of ways to split a binary number such that every part is divisible by 2
Given a binary string S, the task is to find the number of ways to split it into parts such that every part is divisible by 2. Examples: Input: S = "100" Output: 2 There are two ways to split the string: {"10", "0"} and {"100"}Input: S = "110" Output: 1 Approach: One observation is that the string can only be split after a 0. Thus, count the number
7 min read
Count ways to split string into K Substrings starting with even digit and min length M
Given a string S of length N consisting of digits from '0' to '9' .The task is to determine the total number of possible ways to partition the string into K substrings such that : Each substring has a minimum length of M (M&gt;=2).Substring must start with an even digit number and end with an odd digit number. Examples: Input: N = 9, M = 2, k = 3,
22 min read
Count of ways to split an Array into three contiguous Subarrays having increasing Sum
Given an array arr[] consisting of non-negative integers, the task is to find the number of ways to split the array into three non-empty contiguous subarrays such that their respective sum of elements are in increasing order. Examples: Input: arr[] = {2, 3, 1, 7} Output: 2 Explanation: {{2}, {3, 1}, {7}}, {{2}, {3}, {1, 7}} are the possible splits.
7 min read
Count of ways to split N into Triplets forming a Triangle
Given an integer N, the task is to find the number of ways to split N into ordered triplets which can together form a triangle. Examples: Input: N = 15 Output: Total number of triangles possible are 28 Input: N = 9 Output: Total number of triangles possible is 10 Approach: The following observation needs to be made in order to solve the problem: If
4 min read
Count ways to split N! into two distinct co-prime factors
Given an integer N, the task is to find the number of ways N! can be split into two distinct factors A and B such that A and B are co-primes. Since the answer can be very large, print it modulo 109 + 7. Examples: Input: N = 5Output: 4Explanation: The pairs are (1, 120), (3, 40), (5, 24), (8, 15). Input: N = 7Output: 8Explanation: The pairs are (1,
10 min read
Count ways to split array into K non-intersecting subsets
Given an array, arr[] of size N and an integer K, the task is to split the array into K non-intersecting subsets such that union of all the K subsets is equal to the given array. Examples: Input : arr[]= {2, 3}, K=2Output: 4Explanations:Possible ways to partition the array into K(=2) subsets are:{ {{}, {2, 3}}, {{2}, {3}}, {{3}, {2}}, {{2, 3}, {}}
7 min read