Reading Time:- 1 min 55 sec

Reading Time:- 1 min 55 sec

BY TEJAS

Finding the maximum & minimum element in an array is a very simple program. And this program is asked in many interviews. So the following explanation will help you to understand how to tackle this simple program.

ADVERTISEMENT

First, we have created an array called

in which some elements
have been passed. Then the size of an array is stored in the variable
*a[]** n*. Then the first element of an array is stored in the
variables

*min*

and `max`

Then we have created a for loop in which if the value of

is
greater than the variable *a[i]*

then the value of
*max*

should be stored in the variable *a[i]*

& if the value of *max*

is less than the variable
*a[i]** min* So we should store the value of

*a[i]*

in *min*

.
The for loop will continue to be executed until the condition is satisfactory & the
smallest and largest element in the whole array will be stored in the variables

and *min*

.
*max*

And after that, we will simply print the values of

and
*min*

variables.
*max*

ADVERTISEMENT

To find the maximum and minimum element in an array.

` ````
#include<stdio.h>
int main()
{
int a[] = {20, 2, 5, 9, 23, 7, 54};
int n = sisizeof(a[0]);
int min = a[0];
int max = a[0];
for(int i=0; i<n; i++)
{
if(a[i] > max)
max = a[i];
else if(a[i] < min)
min = a[i];
}
printf("Minimum Element in an Array is : %d", min);
printf("Maximum Element in an Array is : %d", max);
}
```

ADVERTISEMENT

To find the maximum and minimum element in an array.

` ````
#include <iostream>
using namespace std;
int main()
{
int a[] = {20, 2, 5, 9, 23, 7, 54};
int n = sizeof(a) / sizeof(a[0]);
int min = a[0];
int max = a[0];
for(int i=0; i<n; i++)
{
if(a[i] > max)
max = a[i];
else if(a[i] < min)
min = a[i];
}
cout << "Minimum Element in an Array is : " << min;
cout << " Maximum Element in an Array is : " << max;
}
```

ADVERTISEMENT

To find the maximum and minimum element in an array.

` ````
public class MinMaxArray {
public static void main(String[] args) {
int[] a = {20, 2, 5, 9, 23, 7, 54};
int n = (int)a.length;
int min = a[0];
int max = a[0];
for(int i = 0; i < n; i++) {
if(a[i] > max) {
max = a[i];
} else if(a[i] < min) {
min = a[i];
}
}
System.out.println("Minimum Element in an Array is : " + min);
System.out.print("Maximum Element in an Array is : " + max);
}
}
```

ADVERTISEMENT

To find the maximum and minimum element in an array.

` ````
a = [20, 2, 5, 9, 23, 7, 54]
n = len(a)
min = max = a[0]
for i in range(n):
if a[i] < max:
max = a[i]
elif a[i] < min:
min = a[i]
print("Minimum Element in an Array is : ", min)
print("Maximum Element in an Array is : ", max)
```

Thank You, For reading this Article. Comment below if you have any doubt or query.

COMMENTS

- A program to reverse an Array or String
- Constructor In Java: A Simple But Complete Guide
- Hierarchy Of Operators / Precedence of Operators
- Inheritance In Java: A Simple But Complete Guide
- What is TensorFlow? A Beginner's Guide to Know about it.
- Linear / Sequential Search Technique
- A Begineer's Guide to Data Visualization in Python

- A program to reverse an Array or String
- Constructor In Java: A Simple But Complete Guide
- Hierarchy Of Operators / Precedence of Operators
- Inheritance In Java: A Simple But Complete Guide
- What is TensorFlow? A Beginner's Guide to Know about it.
- Linear / Sequential Search Technique
- A Begineer's Guide to Data Visualization in Python