Skip to content

Commit 7d4841d

Browse files
authored
Merge pull request #292 from Aiyaan-Mahajan/Aiyaan-Mahajan-Q2DAY2
Aiyaan mahajan q2 day2
2 parents 0ca8530 + 0f98b3f commit 7d4841d

3 files changed

Lines changed: 70 additions & 43 deletions

File tree

-6 KB
Binary file not shown.

Problems/Mathematics/Day-01/sol/Aiyaan_Mahajan/Q1sol.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Q2 Advertising Agency
3+
.*/
4+
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
8+
const int MOD = 1e9 + 7;
9+
10+
long long nCr(int n, int r) {
11+
if (r > n || r < 0) return 0;
12+
if (r == 0 || r == n) return 1;
13+
14+
vector<vector<long long>> dp(n + 1, vector<long long>(r + 1, 0));
15+
16+
for (int i = 0; i <= n; i++) {
17+
dp[i][0] = 1;
18+
for (int j = 1; j <= min(i, r); j++) {
19+
dp[i][j] = (dp[i-1][j-1] + dp[i-1][j]) % MOD;
20+
}
21+
}
22+
23+
return dp[n][r];
24+
}
25+
26+
void solve() {
27+
int n, k;
28+
cin >> n >> k;
29+
30+
vector<int> a(n);
31+
for (int i = 0; i < n; i++) {
32+
cin >> a[i];
33+
}
34+
35+
sort(a.begin(), a.end(), greater<int>());
36+
37+
int kth_value = a[k - 1];
38+
int total_count = 0;
39+
int selected_count = 0;
40+
41+
for (int i = 0; i < n; i++) {
42+
if (a[i] == kth_value) {
43+
total_count++;
44+
if (i < k) {
45+
selected_count++;
46+
}
47+
}
48+
}
49+
50+
long long result = nCr(total_count, selected_count);
51+
cout << result << "\n";
52+
}
53+
54+
int main() {
55+
int t;
56+
cin >> t;
57+
58+
while (t--) {
59+
solve();
60+
}
61+
62+
return 0;
63+
}
64+
65+
66+
//TC : O(n log n)
67+
//SC : O(n²)
68+
/*
69+
My submission : https://codeforces.com/contest/1475/submission/355466435
70+
*/

0 commit comments

Comments
 (0)