Skip to content

Commit 9228cee

Browse files
Added solution to Math/day2/q002
1 parent 7962706 commit 9228cee

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

1.28 KB
Binary file not shown.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SUBMISSION LINK: https://codeforces.com/contest/1475/submission/355447020
2+
3+
/*
4+
PROBLEM STATEMENT:
5+
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers.
6+
In total, Masha has connections of n different bloggers. Blogger numbered i has ai followers.
7+
Since Masha has a limited budget, she can only sign a contract with k different bloggers.
8+
Of course, Masha wants her ad to be seen by as many people as possible.
9+
Therefore, she must hire bloggers with the maximum total number of followers.
10+
Help her, find the number of ways to select k
11+
bloggers so that the total number of their followers is maximum possible.
12+
Two ways are considered different if there is at least one blogger in the first way, which is not in the second way.
13+
Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
14+
*/
15+
16+
/*
17+
Brief Explanation:
18+
We sort the array into descending order. We only need the first k elements.
19+
However there may be multiple bloggers with the least followers (in the top k), thus giving us multiple possibilities.
20+
We need to find combinations containing different bloggers in each case.
21+
Total number of bloggers with the least followers in the top k = count
22+
Total number of bloggers with the least followers needed to fill k = topC.
23+
Number of combinations = C(count, topC)
24+
*/
25+
26+
import java.util.*;
27+
28+
public class Solution2 {
29+
static final int MAX = 1000;
30+
static final long MOD = 1000000007;
31+
32+
static long[][] C = new long[MAX + 1][MAX + 1];
33+
34+
static void combinations() {
35+
for (int i = 0; i <= MAX; i++) {
36+
C[i][0] = C[i][i] = 1;
37+
for (int j = 1; j < i; j++) {
38+
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
39+
}
40+
}
41+
}
42+
public static void main(String[] args) {
43+
Scanner sc = new Scanner (System.in);
44+
combinations();
45+
int t = sc.nextInt();
46+
while (t-- > 0) {
47+
int n = sc.nextInt();
48+
int k = sc.nextInt();
49+
long[] arr = new long[n];
50+
int count = 0;
51+
int topC = 0;
52+
53+
for (int i = 0; i < n; i++) {
54+
arr[i] = sc.nextLong();
55+
}
56+
57+
Arrays.sort(arr);
58+
59+
long mark = arr[n - k];
60+
61+
for (int i = n - 1; i >= 0; i--)
62+
{
63+
if (arr[i] == mark) {
64+
count++;
65+
if (i > n - k - 1)
66+
topC++;
67+
}
68+
}
69+
70+
System.out.println(C[count][topC]);
71+
}
72+
sc.close();
73+
}
74+
}
75+
76+
// Time Complexity = O(n log n)
77+
// precomputing combinations will have time complexity = O(MAX^2)
78+
// Space Complexity = O(MAX^2)

0 commit comments

Comments
 (0)