Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int MOD = 1e9 + 7;
- long long modComb(int n, int r) {
- if (n < r) return 0; // If we cannot choose r elements from n
- return (1LL * n * (n - 1) % MOD * (n - 2) % MOD) % MOD;
- }
- int countTriplets(vector<int>& arr) {
- int n = arr.size();
- int evenCount = 0, oddCount = 0;
- // Count even and odd numbers
- for (int num : arr) {
- if (num % 2 == 0) evenCount++;
- else oddCount++;
- }
- // Total triplets possible: C(n, 3) mod MOD
- long long totalTriplets = modComb(n, 3);
- // Triplets with all odd numbers (invalid): C(oddCount, 3) mod MOD
- long long oddTriplets = modComb(oddCount, 3);
- // Result: (totalTriplets - oddTriplets + MOD) % MOD to prevent negative results
- return (totalTriplets - oddTriplets + MOD) % MOD;
- }
- int main() {
- vector<int> arr = {2, 3, 5, 7, 8}; // Example input
- cout << "Number of valid triplets: " << countTriplets(arr) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement