# Find Smallest and Largest Element of an Array

Find Smallest and Largest Element of 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:

```INPUT:
Arr = {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 and smallest = arr
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 the largest and smallest 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;
int largest = arr;

/* 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```