📌 Problem Description
Write a program to input an integer array and find the maximum and minimum values within it.
- Requirements:
-
Support dynamic input for array length
-
Output the maximum and minimum values
-
Advanced: Minimize the number of comparisons as much as possible
Example:
Input array: 3 9 2 5 7 Output: Maximum=9, Minimum=2
Difficulty:⭐️⭐️ (Basic problem, but optimizing the number of comparisons is challenging)
💡 Basic Implementation (Linear Scan Method)
#include <stdio.h>
void find_min_max(int arr[], int n, int *min, int *max) { if (n == 0) return; // Handle empty array *min = *max = arr[0]; // Initialize
for (int i = 1; i < n; i++) { if (arr[i] < *min) *min = arr[i]; if (arr[i] > *max) *max = arr[i]; }}
int main() { int n; printf("Input array length:"); scanf("%d", &n); int arr[n];
printf("Input %d elements:", n); for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
int min, max; find_min_max(arr, n, &min, &max); printf("Minimum=%d, Maximum=%d", min, max); return 0;}
Output Example:
Input array: 3 9 2 5 7 → Minimum=2, Maximum=9
Key Points:
-
Time Complexity O(n): Traverse the array once
-
Number of Comparisons 2(n-1): Each element is compared twice (to find minimum and maximum)
<span>⚡</span><span> Advanced Optimization (Pairwise Comparison Method)</span>
<span>Reduce the number of comparisons by </span><strong><span>processing elements in pairs</span></strong><span> (Number of comparisons ≈ 1.5n):</span>
void find_min_max_opt(int arr[], int n, int *min, int *max) { if (n == 0) return; int i; // Initialize (handle odd/even length)
if (n % 2 == 1) *min = *max = arr[0], i = 1; else { *min = (arr[0] < arr[1]) ? arr[0] : arr[1]; *max = (arr[0] > arr[1]) ? arr[0] : arr[1]; i = 2; }
// Pairwise comparison for (; i < n; i += 2) { int local_min, local_max; if (arr[i] < arr[i+1]) { local_min = arr[i]; local_max = arr[i+1]; } else { local_min = arr[i+1]; local_max = arr[i]; } if (local_min < *min) *min = local_min; if (local_max > *max) *max = local_max; }}
Optimization Principle:
-
Each time, take two elements and compare their sizes (1 comparison)
-
Compare the smaller one with the current minimum and the larger one with the current maximum (2 comparisons)
-
Total number of comparisons = 3*(n/2) ≈ 1.5n
⚡ Extended Features
1. Return the indices of the maximum/minimum values
void find_min_max_index(int arr[], int n, int *min_idx, int *max_idx) { *min_idx = *max_idx = 0; for (int i = 1; i < n; i++) { if (arr[i] < arr[*min_idx]) *min_idx = i; if (arr[i] > arr[*max_idx]) *max_idx = i; }}
2. Find the second largest number simultaneously
int find_second_max(int arr[], int n) { int max = arr[0], second_max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) { second_max = max; max = arr[i]; } else if (arr[i] > second_max) { second_max = arr[i]; } } return second_max;}
🤔 Common Error Analysis
| Issue | Cause | Solution |
|---|---|---|
Uninitialized<span>min</span>/<span>max</span> |
Did not handle the case when the array length is 0 | Add empty array check |
| Array with all identical elements | Did not consider the case where all elements are equal | Initial values are sufficient to handle this |
| Input data contains non-numeric values | Did not validate<span>scanf</span> return value |
Add input validation loop |
🎯 Today’s Challenge
Try to implement the following features (choose one):
-
Count comparisons: Compare the actual number of comparisons between the linear scan method and the pairwise comparison method
-
Handle floating-point arrays: Support
<span>double</span>type
🚀 Next Preview No. 13: Removing Duplicates from Array Elements: How to Efficiently Remove Duplicates?
📢 Interaction Time
-
What issues have you encountered when finding extreme values? Feel free to leave a comment!
-
Vote: Which method do you think is more efficient?
-
👍 Linear Scan Method (Simple and Intuitive)
-
❤️ Pairwise Comparison Method (Performance Optimized)
If you find this useful, please share it with friends learning programming! 🚀
🚀If you find this useful, feel free to share it with friends learningC programming!
Article Author:Vv Computer Graduate Examination World (Focusing on Computer Graduate Examination Tutoring8 years)
Original Statement: Please contact for authorization if reprinted, infringement will be pursued.