fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. // Function to count the number of set bits in an integer
  6. int countSetBits(int n) {
  7. int count = 0;
  8. while (n) {
  9. count += n & 1;
  10. n >>= 1;
  11. }
  12. return count;
  13. }
  14.  
  15. // Function to count even and odd set bits in an array
  16. void countEvenOddSetBits(const vector<int>& arr, int& evenCount, int& oddCount) {
  17. evenCount = oddCount = 0;
  18. for (int num : arr) {
  19. if (countSetBits(num) % 2 == 0) {
  20. evenCount++;
  21. } else {
  22. oddCount++;
  23. }
  24. }
  25. }
  26.  
  27. int main() {
  28. vector<int> A = {1, 2, 3}; // Example input
  29. vector<int> B = {4, 5, 6};
  30. vector<int> C = {7, 8, 9};
  31.  
  32. int E1, O1, E2, O2, E3, O3;
  33.  
  34. countEvenOddSetBits(A, E1, O1);
  35. countEvenOddSetBits(B, E2, O2);
  36. countEvenOddSetBits(C, E3, O3);
  37.  
  38. long long result = (long long)E1 * E2 * E3 + (long long)E1 * O2 * O3 + (long long)O1 * E2 * O3 + (long long)O1 * O2 * E3;
  39.  
  40. cout << "The number of triplets with an even number of set bits in their XOR is: " << result << endl;
  41.  
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
The number of triplets with an even number of set bits in their XOR is: 14