C Online Compiler
Example: Disjoint Arrays - Brute-Force in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Disjoint Arrays - Brute-Force #include
#include
// For boolean type // Function to check if two arrays are disjoint using brute-force bool areArraysDisjoint_BruteForce(int arr1[], int n1, int arr2[], int n2) { // Step 1: Iterate through each element of the first array for (int i = 0; i < n1; i++) { // Step 2: Iterate through each element of the second array for (int j = 0; j < n2; j++) { // Step 3: If a common element is found, arrays are not disjoint if (arr1[i] == arr2[j]) { return false; // Found a common element } } } // Step 4: If no common elements were found after all comparisons return true; // Arrays are disjoint } int main() { int arr1_a[] = {1, 2, 3, 4}; int n1_a = sizeof(arr1_a) / sizeof(arr1_a[0]); int arr2_a[] = {5, 6, 7}; int n2_a = sizeof(arr2_a) / sizeof(arr2_a[0]); printf("Arrays 1A and 2A: "); if (areArraysDisjoint_BruteForce(arr1_a, n1_a, arr2_a, n2_a)) { printf("Disjoint\n"); } else { printf("Not Disjoint\n"); } int arr1_b[] = {10, 20, 30}; int n1_b = sizeof(arr1_b) / sizeof(arr1_b[0]); int arr2_b[] = {20, 40, 50}; int n2_b = sizeof(arr2_b) / sizeof(arr2_b[0]); printf("Arrays 1B and 2B: "); if (areArraysDisjoint_BruteForce(arr1_b, n1_b, arr2_b, n2_b)) { printf("Disjoint\n"); } else { printf("Not Disjoint\n"); } return 0; }
Output
Clear
ADVERTISEMENTS