# Count possible splits of sum N into K integers such that the minimum is at least P

Given three integers **N**, **P**, and **K**, the task is to find the total number of ways to divide **N **into **K **integers having sum **N **where each integer is â‰¥ **P**.

**Examples:**

Input:K = 3, N = 8, P = 2Output:6Explanation:

Six possible solutions are: {2, 2, 4}, {2, 3, 3}, {2, 4, 2}, {3, 2, 3}, {3, 3, 2}, {4, 2, 2}

Each of the above integers in all combinations are â‰¥ P(= 2) and sum of all the combinations is N(=8).

Input:K = 2, N = 7, P = 2Output:4Explanation:

Four possible solutions are: {4, 3}, {3, 4}, {5, 2}, {2, 5}

Each of the above integers in all combinations are â‰¥ P(= 2) and sum of all the combinations is N(= 7).

**Naive Approach:** The simplest approach is to try all possible combinations of **K** integers having sum **N** and check if they satisfy the above conditions or not. There are **K **positions and in each position, **N **integers can be placed. Therefore, the total number of combinations to check is **N ^{K}**.

**Time Complexity:** O(N^{K})**Auxiliary Space:** O(1)

**Efficient Approach:** To optimize the above approach, the idea is to use the approach discussed in this article and the given above conditions can be written in terms of a linear equation is:

To find the number of solutions for the equation X

_{1 }+ X_{2 }+ X_{3 }+ … + X_{K }= N where X_{i }â‰¥ P.Assume

X._{i}‘ = X_{i}– P

Therefore, the equation reduces to:X_{1}‘ + X_{2}‘ + X_{3}‘ + … + X_{K}‘ = N – K*P

As per the above equations, the given problem reduces to finding total number of ways to split **N – K * P** into **K** integers. Print the total number of ways found above as the required answer.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function that finds the value of` `// the Binomial Coefficient C(n, k)` `int` `binomialCoeff(` `int` `n, ` `int` `k)` `{` ` ` `int` `C[n + 1][k + 1];` ` ` `int` `i, j;` ` ` `// Stores the value of Binomial` ` ` `// Coefficient in bottom up manner` ` ` `for` `(i = 0; i <= n; i++) {` ` ` `for` `(j = 0; j <= min(i, k); j++) {` ` ` `// Base Case` ` ` `if` `(j == 0 || j == i)` ` ` `C[i][j] = 1;` ` ` `// Find the value using` ` ` `// previously stored values` ` ` `else` ` ` `C[i][j] = C[i - 1][j - 1]` ` ` `+ C[i - 1][j];` ` ` `}` ` ` `}` ` ` `// Return the value of C(N, K)` ` ` `return` `C[n][k];` `}` `// Function that count the number of` `// ways to divide N into K integers` `// >= P such that their sum is N` `int` `waysToSplitN(` `int` `k, ` `int` `n, ` `int` `P)` `{` ` ` `// Update the value of N` ` ` `int` `new_N = n - k * P;` ` ` `// Find the binomial coefficient` ` ` `// recursively` ` ` `return` `binomialCoeff(new_N + k - 1,` ` ` `new_N);` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given K, N, and P` ` ` `int` `K = 3, N = 8, P = 2;` ` ` `cout << waysToSplitN(K, N, P);` ` ` `return` `0;` `}` |

## Java

`// Java program for` `// the above approach` `import` `java.util.*;` `class` `GFG{` `// Function that finds the value of` `// the Binomial Coefficient C(n, k)` `static` `int` `binomialCoeff(` `int` `n, ` `int` `k)` `{` ` ` `int` `[][]C = ` `new` `int` `[n + ` `1` `][k + ` `1` `];` ` ` `int` `i, j;` ` ` `// Stores the value of Binomial` ` ` `// Coefficient in bottom up manner` ` ` `for` `(i = ` `0` `; i <= n; i++)` ` ` `{` ` ` `for` `(j = ` `0` `; j <= Math.min(i, k); j++)` ` ` `{` ` ` `// Base Case` ` ` `if` `(j == ` `0` `|| j == i)` ` ` `C[i][j] = ` `1` `;` ` ` `// Find the value using` ` ` `// previously stored values` ` ` `else` ` ` `C[i][j] = C[i - ` `1` `][j - ` `1` `] +` ` ` `C[i - ` `1` `][j];` ` ` `}` ` ` `}` ` ` `// Return the value of C(N, K)` ` ` `return` `C[n][k];` `}` `// Function that count the number of` `// ways to divide N into K integers` `// >= P such that their sum is N` `static` `int` `waysToSplitN(` `int` `k,` ` ` `int` `n, ` `int` `P)` `{` ` ` `// Update the value of N` ` ` `int` `new_N = n - k * P;` ` ` `// Find the binomial coefficient` ` ` `// recursively` ` ` `return` `binomialCoeff(new_N + k - ` `1` `,` ` ` `new_N);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Given K, N, and P` ` ` `int` `K = ` `3` `, N = ` `8` `, P = ` `2` `;` ` ` `System.out.print(waysToSplitN(K, N, P));` `}` `}` `// This code is contributed by 29AjayKumar` |

## Python3

`# Python3 program for the above approach` `# Function that finds the value of` `# the Binomial Coefficient C(n, k)` `def` `binomialCoeff(n, k):` ` ` `C ` `=` `[[` `0` `for` `x ` `in` `range` `(k ` `+` `1` `)]` ` ` `for` `y ` `in` `range` `(n ` `+` `1` `)]` ` ` `# Stores the value of Binomial` ` ` `# Coefficient in bottom up manner` ` ` `for` `i ` `in` `range` `(n ` `+` `1` `):` ` ` `for` `j ` `in` `range` `(` `min` `(i, k) ` `+` `1` `):` ` ` `# Base Case` ` ` `if` `(j ` `=` `=` `0` `or` `j ` `=` `=` `i):` ` ` `C[i][j] ` `=` `1` ` ` ` ` `# Find the value using` ` ` `# previously stored values` ` ` `else` `:` ` ` `C[i][j] ` `=` `(C[i ` `-` `1` `][j ` `-` `1` `] ` `+` ` ` `C[i ` `-` `1` `][j])` ` ` `# Return the value of C(N, K)` ` ` `return` `C[n][k]` `# Function that count the number of` `# ways to divide N into K integers` `# >= P such that their sum is N` `def` `waysToSplitN(k, n, P):` ` ` `# Update the value of N` ` ` `new_N ` `=` `n ` `-` `k ` `*` `P` ` ` `# Find the binomial coefficient` ` ` `# recursively` ` ` `return` `binomialCoeff(new_N ` `+` `k ` `-` `1` `,` ` ` `new_N)` `# Driver Code` `# Given K, N, and P` `K ` `=` `3` `N ` `=` `8` `P ` `=` `2` `print` `(waysToSplitN(K, N, P))` `# This code is contributed by Shivam Singh` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` ` ` `// Function that finds the value of` `// the Binomial Coefficient C(n, k)` `static` `int` `binomialCoeff(` `int` `n, ` `int` `k)` `{` ` ` `int` `[,] C = ` `new` `int` `[n + 1, k + 1];` ` ` `int` `i, j;` ` ` ` ` `// Stores the value of Binomial` ` ` `// Coefficient in bottom up manner` ` ` `for` `(i = 0; i <= n; i++)` ` ` `{` ` ` `for` `(j = 0; j <= Math.Min(i, k); j++)` ` ` `{` ` ` ` ` `// Base Case` ` ` `if` `(j == 0 || j == i)` ` ` `C[i, j] = 1;` ` ` ` ` `// Find the value using` ` ` `// previously stored values` ` ` `else` ` ` `C[i, j] = C[i - 1, j - 1] +` ` ` `C[i - 1, j];` ` ` `}` ` ` `}` ` ` ` ` `// Return the value of C(N, K)` ` ` `return` `C[n, k];` `}` ` ` `// Function that count the number of` `// ways to divide N into K integers` `// >= P such that their sum is N` `static` `int` `waysToSplitN(` `int` `k, ` `int` `n,` ` ` `int` `P)` `{` ` ` ` ` `// Update the value of N` ` ` `int` `new_N = n - k * P;` ` ` ` ` `// Find the binomial coefficient` ` ` `// recursively` ` ` `return` `binomialCoeff(new_N + k - 1,` ` ` `new_N);` `}` ` ` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` ` ` `// Given K, N, and P` ` ` `int` `K = 3, N = 8, P = 2;` ` ` ` ` `Console.Write(waysToSplitN(K, N, P));` `}` `}` `// This code is contributed by sanjoy_62` |

## Javascript

`<script>` `// Javascript program to implement` `// the above approach` `// Function that finds the value of` `// the Binomial Coefficient C(n, k)` `function` `binomialCoeff(n, k)` `{` ` ` `let C = ` `new` `Array(n + 1);` ` ` ` ` `// Loop to create 2D array using 1D array` ` ` `for` `(let i = 0; i < C.length; i++) {` ` ` `C[i] = ` `new` `Array(2);` ` ` `}` ` ` ` ` `let i, j;` ` ` ` ` `// Stores the value of Binomial` ` ` `// Coefficient in bottom up manner` ` ` `for` `(i = 0; i <= n; i++)` ` ` `{` ` ` `for` `(j = 0; j <= Math.min(i, k); j++)` ` ` `{` ` ` `// Base Case` ` ` `if` `(j == 0 || j == i)` ` ` `C[i][j] = 1;` ` ` ` ` `// Find the value using` ` ` `// previously stored values` ` ` `else` ` ` `C[i][j] = C[i - 1][j - 1] +` ` ` `C[i - 1][j];` ` ` `}` ` ` `}` ` ` ` ` `// Return the value of C(N, K)` ` ` `return` `C[n][k];` `}` ` ` `// Function that count the number of` `// ways to divide N into K integers` `// >= P such that their sum is N` `function` `waysToSplitN(k, n, P)` `{` ` ` `// Update the value of N` ` ` `let new_N = n - k * P;` ` ` ` ` `// Find the binomial coefficient` ` ` `// recursively` ` ` `return` `binomialCoeff(new_N + k - 1,` ` ` `new_N);` `}` `// Driver Code` ` ` `// Given K, N, and P` ` ` `let K = 3, N = 8, P = 2;` ` ` ` ` `document.write(waysToSplitN(K, N, P));` ` ` `</script>` |

**Output:**

6

**Time Complexity:** O(N^{2})**Auxiliary Space:** O(N^{2})