How to count the total of all pairs permutations in an array with C

1 Answer

0 votes
#include <stdio.h>

int main()
{
    int arr1[] = {1, 8, 5};
    int arr1_size = sizeof(arr1) / sizeof(arr1[0]);

    // The pairs are: (1, 8), (1, 5), (8, 1), (8, 5), (5, 1), (5, 8)
    
    int total_pairs = arr1_size * (arr1_size - 1);
    printf("Total Pairs = %d\n", total_pairs);

    int arr2[] = {1, 8, 5, 2};
    int arr2_size = sizeof(arr2) / sizeof(arr2[0]);

    // The pairs are: (1, 8), (1, 5), (1, 2), (8, 1), (8, 5), (8, 2),
    //                (5, 1), (5, 8), (5, 2), (2, 1), (2, 8), (2, 5)
    
    total_pairs = arr2_size * (arr2_size - 1);
    printf("Total Pairs = %d\n", total_pairs);

    return 0;
}



/*
run:

Total Pairs = 6
Total Pairs = 12

*/

 



answered Jun 17, 2024 by avibootz
...