Given two values N and K. Find the number of ways to arrange the N distinct items in the boxes such that exactly K (K<N) boxes are used from the N distinct boxes. The answer can be very large so return the answer modulo 109 + 7.
Input: N = 5, k = 5
Input: N = 5, k = 3
Approach: We will use the inclusion-exclusion principle to count the ways.
- Let us assume that the boxes are numbered 1 to N and now we have to choose any K boxes and use them. The number of ways to do this is NCK.
- Now any item can be put in any of the chosen boxes, hence the number of ways to arrange them is KN But here, we may count arrangements with some boxes empty. Hence, we will use the inclusion-exclusion principle to ensure that we count ways with all K boxes filled with at least one item.
Let us understand the application of the inclusion-exclusion principle:
So out of KN ways, we subtract the case when at least 1 box(out of K) is empty. Hence, subtract
- Note that here, The cases where exactly two boxes are empty are subtracted twice(once when we choose the first element in (KC1) ways, and then when we choose the second element in (KC1) ways).
- Hence, we add these ways one time to compensate. So we add (KC2)*((K – 2)N).
- Similarly, here we need to add the number of ways when at least 3 boxes were empty, and so on…
- So out of KN ways, we subtract the case when at least 1 box(out of K) is empty. Hence, subtract
Hence, the total number of ways:
Time complexity: O(N*log N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Color N boxes using M colors such that K boxes have different color from the box on its left
- Color all boxes in line such that every M consecutive boxes are unique
- Program to find the profit or loss when CP of N items is equal to SP of M items
- Probability of distributing M items among X bags such that first bag contains N items
- Count ways to distribute m items among n people
- Count of ways to distribute N items among 3 people with one person receiving maximum
- Number of visible boxes after putting one inside another
- Total number of different staircase that can made from N boxes
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Number of distinct ways to represent a number as sum of K unique primes
- Queries to check if it is possible to join boxes in a circle
- Number of ways to form a heap with n distinct integers
- Number of ways to form an array with distinct adjacent elements
- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Number of ways of distributing N identical objects in R distinct groups
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Find number of ways to form sets from N distinct things with no set of size A or B
- Minimum number of integers required to fill the NxM grid
- Minimum number of square tiles required to fill the rectangular floor
- Minimum number of bottles required to fill K glasses
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.