#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// Check if arrays have the same values regardless of order
// Comparison function for qsort
int compare_ints(const void *a, const void *b) {
int int_a = *(const int *)a;
int int_b = *(const int *)b;
return (int_a > int_b) - (int_a < int_b);
}
// Function to check if two arrays have the same elements regardless of order
bool have_same_values(int arr1[], int arr2[], size_t size) {
if (size == 0) return true; // Empty arrays are considered equal
// Sort both arrays
qsort(arr1, size, sizeof(int), compare_ints);
qsort(arr2, size, sizeof(int), compare_ints);
// Check if arrays are exactly equal (same values & order)
return memcmp(arr1, arr2, size) == 0;
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {4, 3, 5, 2, 1};
size_t size1 = sizeof(arr1) / sizeof(arr1[0]);
size_t size2 = sizeof(arr2) / sizeof(arr2[0]);
// Validate sizes first
if (size1 != size2) {
printf("Arrays are NOT equal (different sizes).\n");
return 0;
}
if (have_same_values(arr1, arr2, size1)) {
printf("Arrays have the SAME values regardless of order.\n");
} else {
printf("Arrays do NOT have the same values.\n");
}
return 0;
}
/*
run:
Arrays have the SAME values regardless of order.
*/