C Online Compiler
Example: Find Symmetric Pairs (Brute-Force) in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Find Symmetric Pairs (Brute-Force) #include
// Structure to represent a pair of integers struct Pair { int first; int second; }; int main() { // Step 1: Define the array of pairs struct Pair pairs[] = {{1, 2}, {3, 4}, {5, 2}, {2, 1}, {4, 3}, {6, 7}}; int n = sizeof(pairs) / sizeof(pairs[0]); printf("Original Pairs:\n"); for (int i = 0; i < n; i++) { printf("(%d, %d) ", pairs[i].first, pairs[i].second); } printf("\n\n"); printf("Symmetric Pairs:\n"); // Step 2: Iterate through each pair using the outer loop for (int i = 0; i < n; i++) { // Step 3: Iterate through subsequent pairs using the inner loop // Starting j from i + 1 avoids checking a pair with itself and duplicate checks for (int j = i + 1; j < n; j++) { // Step 4: Check if pairs[j] is the symmetric counterpart of pairs[i] if (pairs[i].first == pairs[j].second && pairs[i].second == pairs[j].first) { printf("(%d, %d) and (%d, %d)\n", pairs[i].first, pairs[i].second, pairs[j].first, pairs[j].second); } } } return 0; }
Output
Clear
ADVERTISEMENTS