C Program to Find the Maximum and Minimum Element in the Array
Last Updated :
20 Nov, 2024
In this article, we will discuss different ways to find the maximum and minimum elements of the array in C.
The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the current element is smaller or larger respectively.
C
#include <stdio.h>
// Function to find maximum and minimum in an array
void findMinMax(int arr[], int n, int *max, int *min) {
// Assuming first element as minimum and maximum
*max = arr[0];
*min = arr[0];
for (int i = 1; i < n; i++) {
// Update max if arr[i] is larger
if (arr[i] > *max)
*max = arr[i];
// Update min if arr[i] is smaller
if (arr[i] < *min)
*min = arr[i];
}
}
int main() {
int arr[] = {5, 2, 7, 6};
int n = sizeof(arr) / sizeof(arr[0]);
int max, min;
// Finding minimum and maximum values in arr
findMinMax(arr, n, &max, &min);
printf("%d\n", max);
printf("%d\n", min);
return 0;
}
Explanation: We passed the variables to store the minimum and maximum element to findMinMax() function as to avoid the complex techniques to pass multiple values from function.
C also have a few other methods to find the maximum and minimum element of the array that are useful in different cases. Let’s take a look at them one by one:
Using Recursion
In this approach, we search for the maximum and minimum values by checking each element the array using recursion.
C
#include <stdio.h>
// Recursive function to find maximum and minimum
void findMinMax(int arr[], int n, int *max, int *min) {
// Base Case: Only single element left
if (n == 1) {
*max = arr[0];
*min = arr[0];
return;
}
// Recursive call for the rest of the array
findMinMax(arr, n - 1, max, min);
// Update max and min for the current element
if (arr[n - 1] > *max)
*max = arr[n - 1];
if (arr[n - 1] < *min)
*min = arr[n - 1];
}
int main() {
int arr[] = {5, 2, 7, 6};
int n = sizeof(arr) / sizeof(arr[0]);
int max, min;
// Call the recursive function
findMinMax(arr, n, &max, &min);
printf("%d\n", max);
printf("%d", min);
return 0;
}
By Sorting
The largest and smallest element can be easily determined by sorting the array using qsort() function. If the array is sorted in ascending order, the smallest element will be at the beginning (0-th index), and the largest will be at the end (size - 1 index) of the array.
C
#include <stdio.h>
#include <stdlib.h>
// Comparator function for qsort (ascending order)
int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
void findMinMax(int arr[], int n, int *max, int *min) {
// Sort the array using qsort
qsort(arr, n, sizeof(int), compare);
// Minimum element at the start
*min = arr[0];
// Maximum element at the end
*max = arr[n - 1];
}
int main() {
int arr[] = {5, 2, 7, 6};
int n = sizeof(arr) / sizeof(arr[0]);
int min, max;
// Finding minimum and maximum element in the arr
findMinMax(arr, n, &min, &max);
// After sorting, the first element is the smallest
// and the last element is the largest
printf("%d\n", min);
printf("%d", max);
return 0;
}
This method is simple to implement but has a higher time complexity.
Similar Reads
Maximum Size of an Array in C Array in C is a collection of elements of the same data type that are stored in contiguous memory locations. The size of an array is determined by the number of elements it can store and there is a limit on this size. The maximum size of an array in C is determined by many factors, including the dat
4 min read
Maximum Product Subarray in C In this article, we will learn how to find the product of the maximum product subarray for a given array that contains both positive and negative integers,Example:Input: arr[] = [2, 3, -2, 4]Output: 6Explanation: [2, 3] has the largest product 6.Table of ContentMethod 1: Traversing Over Every Subarr
6 min read
Largest Sum Contiguous Subarray in C In this article, we will learn how to find the maximum sum of a contiguous subarray for a given array that contains both positive and negative integers in C language.Example:Input: arr = {-2, -3, 4, -1, -2, 1, 5, -3}Output: 7Explanation: The subarray {4,-1, -2, 1, 5} has the largest sum 7.Maximum su
4 min read
C Program to Find Largest Element in an Array In this article, we will learn how to find the largest element in the array using a C program.The simplest method to find the largest element in the array is by iterating the array and comparing each element with the assumed maximum and updating it when the element is greater.C#include <stdio.h
3 min read
C Program For Maximum and Minimum of an Array Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons. Examples: Input: arr[] = {3, 5, 4, 1, 9}Output: Minimum element is: 1Â Â Â Â Â Â Â Maximum element is: 9 Input: arr[] = {22, 14, 8, 17, 35, 3}Output:Â Minimum eleme
7 min read
C Program to Find Minimum Value in Array In this article, we will learn how to find the minimum value in the array.The easiest and straightforward method is to iterate through each element of the array, comparing each value to an assumed minimum and updating it if the current value is less.C#include <stdio.h> int findMin(int arr[], i
3 min read