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 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 by avibootz

Related questions

2 answers 120 views
120 views asked Apr 13, 2023 by avibootz
1 answer 148 views
148 views asked Jun 13, 2015 by avibootz
1 answer 35 views
35 views asked Jul 12 by avibootz
2 answers 170 views
2 answers 288 views
...