We know that our answer can only lie in the range [0,n]. The position of the numbers actually does not matter, as the number should just be in the array, irrespective of its index. Also, we do not need the duplicates. So, we can make a set of the entire array.
Now, the basic intuition is that, given 'k', it is guaranteed that we can place first k non-negative elements in the array. We just need to check how many elements are there in the array which is less than or equal to k. For each element that is present which is less than or equal to k, we can have 1 element which is greater than k.
So, we can loop through all the elements of the set, ignore the negative elements, and add to k if there is any number less than or equal to k.
At last, we can return min(k, n) as the answer.
#include <bits/stdc++.h>
using namespace std;
#define int long long int
void solve() {
// taking the inputs
int n, k;
cin >> n >> k;
vector<int> a(n);
for(int i=0; i<n; i++){
cin >> a[i];
}
// to handle duplicates and getting the values in sorted order
set<int> s(a.begin(),a.end());
// iterating through all the values
for(auto i:s)
{
// handling negative values
if(i<0) continue;
// increement k if any element is less than or equal to k
if(i<=k)k++;
}
// max possible answer can be n
cout << min(k,n) << endl;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t; cin>>t;
while(t--){
solve();
}
return 0;
}
In this problem, our objective is to count the total number of ways the cricket team can win in the last over, given the total wickets left, total runs left, and total balls left. We can approach this problem using a recursive technique to enumerate all possible winning scenarios.
We can define a recursive function to explore all possible combinations of runs and wickets in the last over. At each step, we consider all possible moves a team can make, including scoring runs, getting out, or facing a dot ball. The recursive function counts the total number of ways the team can win based on the remaining runs, wickets, and balls.
#include "bits/stdc++.h"
using namespace std;
#define VarunDeepSaini ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define int long long
#define double long double
#define endl "\n"
#define all(x) x.begin(), x.end()
#define v vector
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define mpci map<char, int>
#define mpii map<int, int>
#define pb emplace_back
#define mp make_pair
#define F first
#define S second
#define si set<int>
#define msi multiset<int>
#define maxn 1000005
int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b);}
int power(int a, int b, int m) { int ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; }
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
int modInverse(int a, int m) { return power(a, m - 2, m); }
int modAdd(int a, int b, int m) { return ((a % m) + (b % m)) % m; }
int modMul(int a, int b, int m) { return ((a % m) * (b % m)) % m; }
int modSub(int a, int b, int m) { return ((a % m) - (b % m) + m) % m; }
int modDiv(int a, int b, int m) { return (modMul(a, modInverse(b, m), m) + m) % m; }
bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; }
int nCr(int n, int r) { if (r > n - r) r = n - r; int ans = 1; for (int i = 0; i < r; i++) { ans *= (n - i); ans /= (i + 1); } return ans; }
int nCrModP(int n, int r, int p) { if (r > n - r) r = n - r; int C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (int i = 1; i <= n; i++) { for (int j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p; } return C[r]; }
int nPr(int n, int r) { int ans = 1; for (int i = 0; i < r; i++) ans *= (n - i); return ans; }
int nPrModP(int n, int r, int p) { int ans = 1; for (int i = 0; i < r; i++) ans = (ans * (n - i)) % p; return ans; }
int log(int num , int base){int ans = 0; while(num){num /= base;ans++;} return ans;}
int countSetBits(int x){int ans = 0;while(x){ans += (x&1);x >>= 1;}return ans;}
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
bool testcases = 0;
const int mod = 1e9 + 7;
int rec(int runs, int wickets, int balls){
if(runs <= 0) return 1;
if(wickets <= 0) return 0;
if(balls == 0) return 0;
int ans = 0;
ans += rec(runs - 0, wickets, balls - 1);
ans += rec(runs - 1, wickets, balls - 1);
ans += rec(runs - 2, wickets, balls - 1);
ans += rec(runs - 3, wickets, balls - 1);
ans += rec(runs - 4, wickets, balls - 1);
ans += rec(runs - 6, wickets, balls - 1);
ans += rec(runs, wickets - 1, balls - 1);
return ans;
}
void solve()
{
int runs , wickets; cin >> runs >> wickets;
cout << rec(runs, wickets, 6) << endl;
}
int32_t main()
{
VarunDeepSaini
int t = 1;
testcases and cin >> t;
while(t--) solve();
}