Find Largest and Smallest Element in an Array

Find Largest and Smallest Element in an Array

This article is focused to one of the basic problem in Array that is to find the largest and smallest element of an array.

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

Max n min .png

Procedure to follow to get the required sets of result

  1. Declare the required variables.

  2. Take the inputs form the user keyboard.

  3. Assign the max and to the initial or first element of the array.

                  i.e., max = min = a[0]
    
  4. 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.

  5. 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.

                 i.e.,  if (a[i] > max )  then max = a[i]
    
  6. Similarly, if any element found smaller than the previously assigned value, than that element becomes the smallest.

                   i.e.,  if (a[i] < min )  then min = a[i]
    
  7. At last maximum and minimum elements are displayed as per the result output.

Now let's come to the coding part of the problem

Code

#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 | LinkedIn

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

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