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,895 questions

51,826 answers

573 users

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

2 Answers

0 votes
#include <stdio.h>

int countPairs(const int arr[], int size) {
    int count = 0;

    // 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 ((arr[i] & arr[j]) > (arr[i] ^ arr[j])) {
                printf("%d %d\n", arr[i], arr[j]);
                count++;
            }
        }
    }

    return count;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6};
    int size = sizeof(arr) / sizeof(arr[0]);

    int result = countPairs(arr, size);

    printf("Number of pairs where AND exceeds XOR: %d\n", result);

    return 0;
}




/*
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
0 votes
#include <stdio.h>

int countPairs(const int arr[], int n) {
    int count = 0;

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

    return count;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6};
    int size = sizeof(arr) / sizeof(arr[0]);

    int result = countPairs(arr, size);

    printf("Number of pairs where AND exceeds XOR: %d\n", result);

    return 0;
}



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

 



answered Aug 29, 2025 by avibootz

Related questions

2 answers 141 views
141 views asked Apr 13, 2023 by avibootz
1 answer 171 views
171 views asked Jun 13, 2015 by avibootz
1 answer 61 views
61 views asked Jul 12, 2025 by avibootz
2 answers 204 views
2 answers 327 views
...