Incremental Partitioning

You are given two integers N and K, you need to find the number of ways to divide N into k non-empty groups such that size of group[i] >= group[i - 1] for each valid i. Print it modulo 1e9 + 7.

Input format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow.

The first line of  every test case contains 2 space separated integers N and K.
Output format:
For each test case, print the required number of ways modulo 1e9 + 7. 

The output for each test case is printed in a separate line.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function. DON'T forget to print modulo 1e9 + 7.
Constraints:
1 <= T <= 10 
1 <= N <= 1000
1 <= K <= N

Where T is the number of test cases, N is the total number of items and K is the number of groups. 
CodingNinjas
author
2y
Using Recursion
  • We can use a recursive approach to find the number of ways to divide N into K groups incrementally.
  • Our current answer depends on the number of items we have (N), the number of groups we...read more
CodingNinjas
author
2y
Using Memoization

We are solving this problem by solving its subproblems and then combining the solutions of those subproblems. If we analyze carefully, we will see that we are solving the same subprob...read more

CodingNinjas
author
2y
Using Bottom-Up Approach
  • Instead of combining the smaller subproblems, we can solve smaller problems first.
  • We can make use of Dynamic Programming Paradigm to keep track of the previous states to avoid ...read more
CodingNinjas
author
2y
Efficient Approach
  • Consider we are filling the groups in reverse order, i.e. group[i] >= group[i+1].
  • Clearly the Kth group will be the one having minimum size.
  • If it has size of 1, we need to solve the p...read more
Add answer anonymously...
Expedia Group Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter