-
Notifications
You must be signed in to change notification settings - Fork 2
/
Fill up buckets.java
52 lines (41 loc) · 1.65 KB
/
Fill up buckets.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Given n buckets and infinite number of balls. The maximum capacity of each bucket is given in an array capacity[]. Find the number of ways to fill the buckets with balls such that each bucket has atleast 1 ball and all the buckets have distinct number of balls in them.
Note: Since the answer may be very large, calculate the answer modulo 10^9+7.
Example 1:
Input:
n = 1
capacity = [6]
Output: 6
Explanation: Since there is only one
bucket. It may hold any number of balls
ranging from 1 to 6.
Example 2:
Input:
n = 2
capacity = [5, 8]
Output: 35
Explanation: If the first bucket has 1
ball in it then the second bucket cant have 1
ball, so the second bucket has 8-1 = 7 choices.
So the first bucket has 5 choices and for each
choice second bucket has 7 choices.
So total there are 35 ways.
Your Task:
You don't need to read or print anything. Your task is to complete the function totalWays() which takes n and capacity[] as input parameters and returns the number of possible ways to fill the buckets. Since the answer may be very large, calculate the answer modulo 10^9+7.
Expected Time Complexity: O(n*log(n))
Expected Space Complexity: O(1)
Constraints:
1 <= n <= 100000
1 <= capacity[i] <= 100000
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Solution{
public int totalWays(int n, int[] arr) {
// code here
Arrays.sort(arr);
int mod=1000000007;
long ans=1;
for(int i=0;i<n;i++){
ans=(ans*(arr[i]-i))%mod;
}
return (int)ans%mod;
}
}