Advertisement
Valkyrie006

Untitled

Feb 10th, 2025 (edited)
40
0
13 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. long long modComb(int n, int r) {
  9.     if (n < r) return 0; // If we cannot choose r elements from n
  10.     return (1LL * n * (n - 1) % MOD * (n - 2) % MOD) % MOD;
  11. }
  12.  
  13. int countTriplets(vector<int>& arr) {
  14.     int n = arr.size();
  15.     int evenCount = 0, oddCount = 0;
  16.  
  17.     // Count even and odd numbers
  18.     for (int num : arr) {
  19.         if (num % 2 == 0) evenCount++;
  20.         else oddCount++;
  21.     }
  22.  
  23.     // Total triplets possible: C(n, 3) mod MOD
  24.     long long totalTriplets = modComb(n, 3);
  25.    
  26.     // Triplets with all odd numbers (invalid): C(oddCount, 3) mod MOD
  27.     long long oddTriplets = modComb(oddCount, 3);
  28.  
  29.     // Result: (totalTriplets - oddTriplets + MOD) % MOD to prevent negative results
  30.     return (totalTriplets - oddTriplets + MOD) % MOD;
  31. }
  32.  
  33. int main() {
  34.     vector<int> arr = {2, 3, 5, 7, 8}; // Example input
  35.     cout << "Number of valid triplets: " << countTriplets(arr) << endl;
  36.     return 0;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement