Find Largest and Smallest Element in an Array
Tags: codenewbie, cpp, programming, computerscience
Published: 2021-06-05 09:09:02

Hello guys👋, In this post we gonna check another most basic but most important programming problem in which we have to **find the Largest and Smallest Element in an Array**. This problem❓ I solved when I was practicing✍ for the concept Array and thought🤔 that it will be useful if I share this problem with all those who are currently learning and practicing the Coding👨‍💻.

### Problem Description
Write a program to find the largest and smallest element in an array.

**Sample input:**

6

20 25 100 95 45 5

**Sample output:**

The largest element in the array is : 100

The smallest element in the array is : 5
![Largest and Smallest Element in an Array](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1pjq1bcfw6h3aycpzdt.png)

### Procedure to follow to get the required sets of result
<ol>
<li>Declare the required variables.</li>

<li>Take the inputs form the user keyboard.</li>

<li>Assign the max and to the initial or first element of the array.</li>

```
              i.e., max = min = a[0]
```

<li>If there is only one element present in the array then the loop did not execute and max, min hold the only present value in the array, thus that element becomes the both maximum and minimum.</li>

<li>If array has more than one element, than loop executes and if any element found bigger than the previously assigned value, then that element becomes the largest.</li>

```
             i.e.,  if (a[i] > max )  then max = a[i]
```

<li>Similarly, if any element found smaller than the previously assigned value, than that element becomes the smallest.</li>

```
               i.e.,  if (a[i] < min )  then min = a[i]
```

<li>At last maximum and minimum elements are displayed as per the result output.</li>

### Now let's come to the coding👨‍💻 part of the problem
#### Code
```

/*Code is written in C++ language*/
  #include <iostream>
  using namespace std;
  int main() {
    int n,min,max;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    min=max=arr[0];
    for(int i=0;i<n;i++)
    {
        if(arr[i]<min)
        {
            min=arr[i];
        }
        if(arr[i]>max)
        {
            max=arr[i];
        }
     }
    cout<<"The largest element in the array is : "<<max;
    cout<<endl;
    cout<<"The smallest element in the array is : "<<min;
    return 0;
  }

```
The same set of code is compiled on the online compiler to provide the outputs as per the condition asked in the problem.

**Input test**
```

6
20 25 100 95 45 5

```
**Output test**
```

The largest element in the array is : 100
The smallest element in the array is : 5

```
Hence with the required set of problem and its explaination we came across to know one of the important programming problem in Array and we can make the concept array strong with practicing such kinds of problem as much as we can.

Hope with this you learned and acquired some basic knowledge and help you somewhat.

Drop a Love❤ if you liked👍 this post, then share 🤝this with your friends and if anything is confusing or incorrect then let me know in the comment section.

#### I would love to connect with you all [Twitter](twitter.com/mayankp4513) | [LinkedIn](https://www.linkedin.com/in/mayank-pathak4513/)

If you find the post useful then consider **[Buying me a coffee](https://www.buymeacoffee.com/mayankpathak)☕**
   <a href="https://www.buymeacoffee.com/mayankpathak" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>

Thanks from my side, this is Mayank, keep learning and exploring !!

Meet you in the next article......till than see ya🤚