DEV Community

Cover image for Find Largest and Smallest Element in an Array
Mayank Pathak
Mayank Pathak

Posted on • Originally published at thecodingbro.xyz

Find Largest and Smallest Element in 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
Largest and Smallest Element in an Array

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.
  4.               i.e., max = min = a[0]
    
  5. 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.
  6. 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.
  7.              i.e.,  if (a[i] > max )  then max = a[i]
    
  8. Similarly, if any element found smaller than the previously assigned value, than that element becomes the smallest.
  9.                i.e.,  if (a[i] < min )  then min = a[i]
    
  10. At last maximum and minimum elements are displayed as per the result output.
  11. 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 | LinkedIn

    If you find the post useful then consider Buying me a coffee
    Buy Me A Coffee

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

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

Top comments (2)

Collapse
 
pgradot profile image
Pierre Gradot
Collapse
 
mayankpathak profile image
Mayank Pathak

Sure from next time :)