# 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:**

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

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

- Set largest = arr[0] and smallest = arr[0]
- 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; }

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 5OUTPUT:O(n), where n is the size of the arrayTime Complexity:

**Related Posts:**

**Program to count occurrences of a particular element in an array.****Program to search an element in an Array (Linear Search).****Program to Rotate Array Elements by ‘D’ positions.****Program to find most frequent element in an array.****Program to find pair in an array with given sum.****Program to find first repeating element of an array.****Program to merge two sorted arrays.****Program to find missing number in an array.****Program to sort if array is sorted.****Program to print Alternate Elements of an Array.****Program to swap kth element from beginning to kth element from end in an Array.****Program to print all possible subarrays of the given array.****Program to print kth smallest and kth largest element of an Array.****Program to find equilibrium index of an Array.****Program to find majority element of an Array.****Program to find mean of the Array.****Program to sort an Array of 0s and 1s.****Program to Reverse an Array.****Program to count number of odd numbers and even numbers in an array.****Program to Reverse an Array.**