Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to count pairs from a given vector where the bitwise AND of the two numbers is greater than the bitwise XOR in C++

2 Answers

0 votes
#include <iostream>
#include <vector>

int countPairs(const std::vector<int>& vec) {
    int count = 0;
    int size = vec.size();

    // Loop through every pair
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (i == j) continue;

            // Check the condition: AND > XOR
            if ((vec[i] & vec[j]) > (vec[i] ^ vec[j])) {
                std::cout << vec[i] << " " << vec[j] << "\n";
                count++;
            }
        }
    }

    return count;
}

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5, 6};  

    int result = countPairs(vec);

    std::cout << "Number of pairs where AND exceeds XOR: " << result << "\n";
}

 
 
/*
run:
 
2 3
3 2
4 5
4 6
5 4
5 6
6 4
6 5
Number of pairs where AND exceeds XOR: 8
 
*/

 



answered Aug 29, 2025 by avibootz
edited Aug 29, 2025 by avibootz
0 votes
#include <iostream>
#include <vector>

int countPairs(const std::vector<int>& vec) {
    int count = 0;
    int size = vec.size();

    // Loop through each unique pair (i < j)
    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            // Check the condition: AND > XOR
            if ((vec[i] & vec[j]) > (vec[i] ^ vec[j])) {
                printf("%d %d\n", vec[i], vec[j]);
                count++;
            }
        }
    }

    return count;
}

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5, 6};  

    int result = countPairs(vec);

    std::cout << "Number of pairs where AND exceeds XOR: " << result << "\n";
}

 
 
/*
run:
 
2 3
4 5
4 6
5 6
Number of pairs where AND exceeds XOR: 4
 
*/

 



answered Aug 29, 2025 by avibootz
...