Arrays are fundamental data structures in programming, widely used for storing collections of elements. One common problem encountered when working with arrays is finding the smallest and largest element efficiently.
This article covers three different approaches to solving this problem, along with detailed explanations, algorithms, code implementations, and time complexities.
Given an array of numbers, we need to determine:
arr = {7, 2, 9, 4, 1, 5}
Smallest element: 1
Largest element: 9
One of the simplest ways to find the smallest and largest numbers in an array is by iterating through the array and keeping track of both values.
small = arr[0]
(first element as the smallest)large = arr[0]
(first element as the largest)1
to n-1
.arr[i] > large
, update large = arr[i]
.arr[i] < small
, update small = arr[i]
.small
and large
.#include <iostream>
using namespace std;
void findMinMax(int arr[], int n) {
int small = arr[0], large = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > large)
large = arr[i];
if (arr[i] < small)
small = arr[i];
}
cout << "Smallest element: " << small << endl;
cout << "Largest element: " << large << endl;
}
int main() {
int arr[] = {7, 2, 9, 4, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
findMinMax(arr, n);
return 0;
}
Instead of iterating through the array, we can use recursion to find the smallest and largest elements.
#include <iostream>
using namespace std;
void findMinMaxRecursive(int arr[], int index, int n, int &small, int &large) {
if (index == n) return;
if (arr[index] > large)
large = arr[index];
if (arr[index] < small)
small = arr[index];
findMinMaxRecursive(arr, index + 1, n, small, large);
}
int main() {
int arr[] = {7, 2, 9, 4, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int small = arr[0], large = arr[0];
findMinMaxRecursive(arr, 1, n, small, large);
cout << "Smallest element: " << small << endl;
cout << "Largest element: " << large << endl;
return 0;
}
We can also sort the array using C++ Standard Template Library (STL) and directly retrieve the first and last elements as the smallest and largest elements, respectively.
sort()
function.arr[0]
) will be the smallest.arr[n-1]
) will be the largest.#include <iostream>
#include <algorithm>
using namespace std;
void findMinMaxUsingSorting(int arr[], int n) {
sort(arr, arr + n);
cout << "Smallest element: " << arr[0] << endl;
cout << "Largest element: " << arr[n - 1] << endl;
}
int main() {
int arr[] = {7, 2, 9, 4, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
findMinMaxUsingSorting(arr, n);
return 0;
}
Method | Time Complexity | Space Complexity | Efficiency |
---|---|---|---|
Iterative (Linear Search) | O(n) | O(1) | ✅ Best (Most Efficient) |
Recursive Approach | O(n) | O(n) (due to recursion) | ❌ Less efficient |
Sorting Approach (STL Sort) | O(n log n) | O(1) | ❌ Least efficient |
✔ Iterative method is the most efficient approach with O(n) time complexity.
✔ Recursive method is useful but less memory-efficient due to function call overhead.
✔ Sorting method is not recommended unless sorting is required for other operations.
✔ Use STL functions like min_element()
and max_element()
for a concise solution.
Yes! You can use min_element()
and max_element()
from the <algorithm>
library:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {7, 2, 9, 4, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Smallest element: " << *min_element(arr, arr + n) << endl;
cout << "Largest element: " << *max_element(arr, arr + n) << endl;
return 0;
}
This method works in O(n) time, similar to the iterative method.
The smallest and largest elements will be the same since all elements are equal.
Yes, by using the iterative approach with O(n) time complexity.
Would you like a flowchart or diagram to better
Each method has its advantages and use cases. The iterative approach is the most efficient in terms of both time and space complexity. The recursive method is useful in recursive programming scenarios, while the sorting method is ideal when the data needs to be sorted anyway.