Array Applications (Finding Max/Min and Second Max/Min)
Learn about how we can find the maximum and minimum in an array.
Let’s see some examples to experience the practical usage of arrays.
Finding the maximum and minimum in an array
Let us solve first by finding the maximum number from the array.
Problem 1: Finding the maximum
As an example, we have written a program below with a given array of integers for finding the maximum number from the array.
A[] = {1 2 93 -4 75 6 8 23 }
Max: 93
The idea is simple:
- We assume the first element to be the maximum number
int max = A[0]
. - We traverse the array with an index variable
ai=1
till the last entry atsize-1
.- We compare
max
with each element and update it whenevermax
is less than theA[ai]
.
- We compare
Get hands-on with 1200+ tech skills courses.