This article discusses the program used to find the smallest and largest element in an array. Given an array, the objective of the program will be to find the largest and smallest elements.
Method 1: Traverse the array iteratively and keep track of the smallest and largest element until the end of the array.
Method 2: Traverse the array recursively and keep track of the smallest and largest element until the end of the array.
Method 3: Sort the array using STL and return the first element as the smallest element and the last element as the largest element.
For example, consider the array.
arr = {1, 2, 3, 4, 5}
Smallest element : 1
Largest element : 5
@@coding::1@@
Time complexity: O(n)
@@coding::2@@
Time complexity: O(n)
@@coding::3@@
Time complexity: O(n log n)