File tree Expand file tree Collapse file tree
Day-01/sol/Aiyaan_Mahajan
Day-02/sol/Aiyaan_Mahajan Expand file tree Collapse file tree Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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+ */
You can’t perform that action at this time.
0 commit comments