Find smallest and largest element in an array | FACE Prep

Find smallest and largest element in an array | FACE Prep

Finding the Smallest and Largest Element in an Array

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.

Problem Statement

Given an array of numbers, we need to determine:

  • The smallest (minimum) element in the array
  • The largest (maximum) element in the array

Example

Input:

arr = {7, 2, 9, 4, 1, 5}

Output:

Smallest element: 1
Largest element: 9

Approach 1: Iterative Method (Linear Search)

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.

Algorithm

  1. Initialize two variables:
    • small = arr[0] (first element as the smallest)
    • large = arr[0] (first element as the largest)
  2. Traverse through the array from index 1 to n-1.
  3. If arr[i] > large, update large = arr[i].
  4. If arr[i] < small, update small = arr[i].
  5. Print the values of small and large.

Code Implementation (C++)

#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;
}

Time Complexity:

  • O(n) → The array is traversed once, making this method efficient.

Approach 2: Recursive Method

Instead of iterating through the array, we can use recursion to find the smallest and largest elements.

Algorithm

  1. Base Case: If there is only one element, return it as both smallest and largest.
  2. Recursive Case: Compare the first element with the result of the recursive call on the rest of the array.
  3. Keep track of the minimum and maximum while making recursive calls.

Code Implementation (C++)

#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;
}

Time Complexity:

  • O(n) → Recursive traversal through the array.

Space Complexity:

  • O(n) → Due to recursive function calls (stack memory).

Approach 3: Sorting Method (Using STL in C++)

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.

Algorithm

  1. Sort the array using the sort() function.
  2. The first element (arr[0]) will be the smallest.
  3. The last element (arr[n-1]) will be the largest.

Code Implementation (C++)

#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;
}

Time Complexity:

  • O(n log n) → Sorting an array takes O(n log n) time, making this method less efficient than the previous ones.

Comparison of All Three Methods

MethodTime ComplexitySpace ComplexityEfficiency
Iterative (Linear Search)O(n)O(1)✅ Best (Most Efficient)
Recursive ApproachO(n)O(n) (due to recursion)❌ Less efficient
Sorting Approach (STL Sort)O(n log n)O(1)❌ Least efficient

Key Takeaways

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.


Frequently Asked Questions (FAQs)

1. Can I use STL to find min/max without sorting?

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.

2. What happens if all elements in the array are the same?

The smallest and largest elements will be the same since all elements are equal.

3. Can I find min/max in an unsorted array efficiently?

Yes, by using the iterative approach with O(n) time complexity.


Would you like a flowchart or diagram to better


Conclusion

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.

Finding the Smallest and Largest Element in an Array