Given an array arr[] of n elements, find the largest element present in the array.
Examples
Input: arr[] = {5, 2, 7, 6}
Output: 7
Explanation: Among all the elements in the array, 7 is the largest.Input: arr[] = {10, 25, 3, 8, 15}
Output: 25
Explanation: Among all the elements in the array, 25 is the largest.
Table of Content
Using Sorting - O(n log n) Time, O(log n) Auxiliary Space
The idea is to sort the array in ascending order using qsort(). After sorting, the last element of the array is the largest element.
#include
#include
// Comparator for ascending order
int compare(const void *a, const void *b)
{
return (*(int *)a - *(int *)b);
}
int findMax(int arr[], int n)
{
// Sort the array in ascending order
qsort(arr, n, sizeof(int), compare);
// Last element is the largest
return arr[n - 1];
}
int main()
{
int arr[] = {5, 2, 7, 6};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", findMax(arr, n));
return 0;
}
Output
7
Explanation
- qsort() arranges the elements in ascending order.
- After sorting, arr[n - 1] contains the largest element.
- The sorted array is modified in the process.
Using Recursion - O(n) Time, O(n) Auxiliary Space
The recursive approach finds the maximum in the first n - 1 elements and compares it with the last element.
- If the array contains one element, return that element.
- Recursively find the maximum among the first n - 1 elements.
- Compare the result with arr[n - 1].
- Return the larger value.
#include
// Recursive function to find the maximum element
int findMax(int arr[], int n)
{
// Base case
if (n == 1)
return arr[0];
// Find maximum in the first n - 1 elements
int max = findMax(arr, n - 1);
// Compare with the current element
return arr[n - 1] > max ? arr[n - 1] : max;
}
int main()
{
int arr[] = {5, 2, 7, 6};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", findMax(arr, n));
return 0;
}
Output
7
Explanation
- findMax() recursively processes smaller portions of the array.
- Each call returns the larger value between the maximum found so far and the current element.
- For {5, 2, 7, 6}, the final returned value is 7.
Using Linear Traversal - O(n) Time, O(1) Auxiliary Space
The simplest approach is to assume the first element as the largest and compare it with the remaining elements. Update the maximum whenever a larger element is found.
- Initialize max with the first element.
- Traverse the remaining elements.
- Compare each element with max.
- Update max if a larger element is found.
- Return max.
#include
int findMax(int arr[], int n)
{
// Assume the first element is the largest
int max = arr[0];
// Compare remaining elements with max
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
int main()
{
int arr[] = {5, 2, 7, 6};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", findMax(arr, n));
return 0;
}
Output
7
Explanation
- max initially stores 5 and is later updated to 7.
- sizeof(arr) / sizeof(arr[0]) calculates the number of elements as 4.
- findMax() returns 7, which is printed by main().
Note: Linear traversal is the most efficient approach for finding the largest element because it checks each element exactly once without modifying the array or using extra data structures.