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, 2}Input : N = 23, K = 4, L = 2, R = 10
Output : 480
Naive Approach
We can solve the problem using recursion. At each step of recursion try to make a group of size L to R all
There will be two arguments in the recursion which changes:
- pos – the group number
- left – how much of N is left to be distributed
Below is the implementation of the above approach:
C++
// C++ implementation to count the// number of ways to divide N in K// groups such that each group// has elements in range [L, R]#include <bits/stdc++.h>using namespace std;const int mod = 1000000007;// Function to count the number// of ways to divide the number N// in K groups such that each group// has number of elements in range [L, R]int calculate(int pos, int left, int k, int L, int R){ // Base Case if (pos == k) { if (left == 0) return 1; else return 0; } // if N is divides completely // into less than k groups if (left == 0) return 0; int answer = 0; // put all possible values // greater equal to prev for (int i = L; i <= R; i++) { if (i > left) break; answer = (answer + calculate(pos + 1, left - i, k, L, R)) % mod; } return answer;}// Function to count the number of// ways to divide the number Nint countWaystoDivide(int n, int k, int L, int R){ return calculate(0, n, k, L, R);}// Driver Codeint main(){ int N = 12; int K = 3; int L = 1; int R = 5; cout << countWaystoDivide(N, K, L, R); return 0;} |
Java
// Java implementation to count the// number of ways to divide N in K// groups such that each group// has elements in range [L, R]class GFG{static int mod = 1000000007;// Function to count the number// of ways to divide the number N// in K groups such that each group// has number of elements in range [L, R]static int calculate(int pos, int left, int k, int L, int R){ // Base Case if (pos == k) { if (left == 0) return 1; else return 0; } // If N is divides completely // into less than k groups if (left == 0) return 0; int answer = 0; // Put all possible values // greater equal to prev for(int i = L; i <= R; i++) { if (i > left) break; answer = ((answer + calculate(pos + 1,left - i, k, L, R)) % mod); } return answer;}// Function to count the number of// ways to divide the number Nstatic int countWaystoDivide(int n, int k, int L, int R){ return calculate(0, n, k, L, R);}// Driver Codepublic static void main(String[] args){ int N = 12; int K = 3; int L = 1; int R = 5; System.out.print(countWaystoDivide(N, K, L, R));}}// This code is contributed by Amit Katiyar |
Python3
# Python3 implementation to count the # number of ways to divide N in K # groups such that each group # has elements in range [L, R] mod = 1000000007# Function to count the number # of ways to divide the number N # in K groups such that each group # has number of elements in range [L, R] def calculate(pos, left, k, L, R): # Base Case if (pos == k): if (left == 0): return 1 else: return 0 # If N is divides completely # into less than k groups if (left == 0): return 0 answer = 0 # Put all possible values # greater equal to prev for i in range(L, R + 1): if (i > left): break answer = (answer + calculate(pos + 1, left - i, k, L, R)) % mod return answer# Function to count the number of # ways to divide the number N def countWaystoDivide(n, k, L, R): return calculate(0, n, k, L, R)# Driver Code N = 12K = 3L = 1R = 5print(countWaystoDivide(N, K, L, R)) # This code is contributed by divyamohan123 |
C#
// C# implementation to count the// number of ways to divide N in K// groups such that each group// has elements in range [L, R]using System;class GFG{static int mod = 1000000007;// Function to count the number// of ways to divide the number N// in K groups such that each group// has number of elements in range [L, R]static int calculate(int pos, int left, int k, int L, int R){ // Base Case if (pos == k) { if (left == 0) return 1; else return 0; } // If N is divides completely // into less than k groups if (left == 0) return 0; int answer = 0; // Put all possible values // greater equal to prev for(int i = L; i <= R; i++) { if (i > left) break; answer = ((answer + calculate(pos + 1,left - i, k, L, R)) % mod); } return answer;}// Function to count the number of// ways to divide the number Nstatic int countWaystoDivide(int n, int k, int L, int R){ return calculate(0, n, k, L, R);}// Driver Codepublic static void Main(String[] args){ int N = 12; int K = 3; int L = 1; int R = 5; Console.Write(countWaystoDivide(N, K, L, R));}}// This code is contributed by Amit Katiyar |
10
Time Complexity: O(NK).
Efficient Approach: In the previous approach we can see that we are solving the subproblems repeatedly, i.e. it follows the property of Overlapping Subproblems. So we can memoize the same using DP table.
Below is the implementation of the above approach:
C++
// C++ implementation to count the// number of ways to divide N in K// groups such that each group// has elements in range [L, R]#include <bits/stdc++.h>using namespace std;const int mod = 1000000007;// DP Tableint dp[1000][1000];// Function to count the number// of ways to divide the number N// in K groups such that each group// has number of elements in range [L, R]int calculate(int pos, int left, int k, int L, int R){ // Base Case if (pos == k) { if (left == 0) return 1; else return 0; } // if N is divides completely // into less than k groups if (left == 0) return 0; // If the subproblem has been // solved, use the value if (dp[pos][left] != -1) return dp[pos][left]; int answer = 0; // put all possible values // greater equal to prev for (int i = L; i <= R; i++) { if (i > left) break; answer = (answer + calculate(pos + 1, left - i, k, L, R)) % mod; } return dp[pos][left] = answer;}// Function to count the number of// ways to divide the number Nint countWaystoDivide(int n, int k, int L, int R){ // Intialize DP Table as -1 memset(dp, -1, sizeof(dp)); return calculate(0, n, k, L, R);}// Driver Codeint main(){ int N = 12; int K = 3; int L = 1; int R = 5; cout << countWaystoDivide(N, K, L, R); return 0;} |
Java
// Java implementation to count the// number of ways to divide N in K// groups such that each group// has elements in range [L, R]class GFG{static int mod = 1000000007;// DP Tablestatic int [][]dp = new int[1000][1000];// Function to count the number// of ways to divide the number N// in K groups such that each group// has number of elements in range [L, R]static int calculate(int pos, int left, int k, int L, int R){ // Base Case if (pos == k) { if (left == 0) return 1; else return 0; } // If N is divides completely // into less than k groups if (left == 0) return 0; // If the subproblem has been // solved, use the value if (dp[pos][left] != -1) return dp[pos][left]; int answer = 0; // Put all possible values // greater equal to prev for(int i = L; i <= R; i++) { if (i > left) break; answer = (answer + calculate(pos + 1, left - i, k, L, R)) % mod; } return dp[pos][left] = answer;}// Function to count the number of// ways to divide the number Nstatic int countWaystoDivide(int n, int k, int L, int R){ // Intialize DP Table as -1 for(int i = 0; i < 1000; i++) { for(int j = 0; j < 1000; j++) { dp[i][j] = -1; } } return calculate(0, n, k, L, R);}// Driver Codepublic static void main(String[] args){ int N = 12; int K = 3; int L = 1; int R = 5; System.out.print(countWaystoDivide(N, K, L, R));}}// This code is contributed by 29AjayKumar |
Python3
# Python3 implementation to count the# number of ways to divide N in K# groups such that each group# has elements in range [L, R]mod = 1000000007 # DP Tabledp = [[-1 for j in range(1000)] for i in range(1000)] # Function to count the number# of ways to divide the number N# in K groups such that each group# has number of elements in range [L, R]def calculate(pos, left, k, L, R): # Base Case if (pos == k): if (left == 0): return 1 else: return 0 # if N is divides completely # into less than k groups if (left == 0): return 0 # If the subproblem has been # solved, use the value if (dp[pos][left] != -1): return dp[pos][left] answer = 0 # put all possible values # greater equal to prev for i in range(L, R + 1): if (i > left): break answer = (answer + calculate(pos + 1, left - i, k, L, R)) % mod dp[pos][left] = answer return answer# Function to count the number of# ways to divide the number Ndef countWaystoDivide(n, k, L, R): return calculate(0, n, k, L, R)# Driver codeif __name__=="__main__": N = 12 K = 3 L = 1 R = 5 print(countWaystoDivide(N, K, L, R)) # This code is contributed by rutvik_56 |
C#
// C# implementation to count the// number of ways to divide N in K// groups such that each group// has elements in range [L, R]using System;class GFG{static int mod = 1000000007;// DP Tablestatic int [,]dp = new int[1000, 1000];// Function to count the number// of ways to divide the number N// in K groups such that each group// has number of elements in range [L, R]static int calculate(int pos, int left, int k, int L, int R){ // Base Case if (pos == k) { if (left == 0) return 1; else return 0; } // If N is divides completely // into less than k groups if (left == 0) return 0; // If the subproblem has been // solved, use the value if (dp[pos, left] != -1) return dp[pos, left]; int answer = 0; // Put all possible values // greater equal to prev for(int i = L; i <= R; i++) { if (i > left) break; answer = (answer + calculate(pos + 1, left - i, k, L, R)) % mod; } return dp[pos, left] = answer;}// Function to count the number of// ways to divide the number Nstatic int countWaystoDivide(int n, int k, int L, int R){ // Intialize DP Table as -1 for(int i = 0; i < 1000; i++) { for(int j = 0; j < 1000; j++) { dp[i, j] = -1; } } return calculate(0, n, k, L, R);}// Driver Codepublic static void Main(){ int N = 12; int K = 3; int L = 1; int R = 5; Console.Write(countWaystoDivide(N, K, L, R));}}// This code is contributed by Code_Mech |
10
Time Complexity: O(N2).
Recommended Posts:
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Count of ways to split a given number into prime segments
- Number of ways to split a binary number such that every part is divisible by 2
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Count of ways to split given string into two non-empty palindromes
- Find all possible ways to Split the given string into Primes
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Split a number as sum of K numbers which are not divisible by K
- Number of ways to represent a number as sum of k fibonacci numbers
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Split N^2 numbers into N groups of equal sum
- Number of ways to get a given sum with n number of m-faced dices
- Number of ways to select exactly K even numbers from given Array
- Split the array into equal sum parts according to given conditions
- Split a given string into substrings of length K with equal sum of ASCII values
- Split array into minimum number of subsets having maximum pair sum at most K
- Count of Ways to obtain given Sum from the given Array elements
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Queries for elements having values within the range A to B in the given index range using Segment Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


