Find Smallest and Largest Element in an Array

Find Smallest and Largest Element in an Array” is an elementary problem of an Array data structure for beginners. Here, we are given an array of size ‘n’ and our task is to find the largest and smallest element of the array.

Example (Find smallest and Largest Element in an Array):

INPUT:
Arr[5] = {4, 1, 3, 2, 5}
OUTPUT:
Largest Number = 5
Smallest Number = 1

The steps to find the largest and smallest elements of the array are as follows:

  1. Set largest = arr[0] and smallest = arr[0]
  2. Scan the array element from index 1 to (size-1). For each element, 

Check if arr[i] > largest, then largest = arr[i]

and

Check if arr[i] < smallest, then smallest = arr[i]

3.Print the smallest and largest number.

C++ Program to find Smallest and Largest Element in an Array is as follows:

/* Program to find smallest and largest element in an array */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    int n;  
      
    /* Scanning size of the array */  
    cout<<"Enter the number of array elements: ";  
    cin>>n;  
      
    /* Creating an Array */  
    int arr[n];  
      
    /* Scanning the array elements */  
    cout<<"\nEnter the array elements: ";  
    for(int i = 0; i < n; i++)  
    cin>>arr[i];  
      
    int smallest = arr[0];  
    int largest = arr[0];  
      
    /* Finding smallest and largest numbers */  
    for(int i = 1; i < n; i++)  
    {  
        if(arr[i] > largest)  
        largest = arr[i];  
          
        if(arr[i] < smallest)  
        smallest =  arr[i];  
    }  
      
    /* Printing the result */  
    cout<<"\nThe smallest number in an array is "<<smallest;  
    cout<<"\nThe largest number in an array is "<<largest;  
}  

OUTPUT:
Enter the number of array elements: 5
Enter the array elements: 2 5 3 1 4
The smallest number in an array is 1
The largest number in an array is 5
Time Complexity:
O(n), where n is the size of the array

Related Posts:

  1. Program to count occurrences of a particular element in an array.
  2. Program to search an element in an Array (Linear Search).
  3. Program to Rotate Array Elements by ‘D’ positions.
  4. Program to find most frequent element in an array.
  5. Program to find pair in an array with given sum.
  6. Program to find first repeating element of an array.
  7. Program to merge two sorted arrays.
  8. Program to find missing number in an array.
  9. Program to sort if array is sorted.
  10. Program to print Alternate Elements of an Array.
  11. Program to swap kth element from beginning to kth element from end in an Array.
  12. Program to print all possible subarrays of the given array.
  13. Program to print kth smallest and kth largest element of an Array.
  14. Program to find equilibrium index of an Array.
  15. Program to find majority element of an Array.
  16. Program to find mean of the Array.
  17. Program to sort an Array of 0s and 1s.
  18. Program to Reverse an Array.
  19. Program to count number of odd numbers and even numbers in an array.
  20. Program to Reverse an Array.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *