Program to find the minimum (or maximum) element of an array in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] of n integers. Our task is to create a program to find the minimum and maximum element of an array in C++.

Problem Description − Here, we have an array arr[]. The contains n integer values. We have to find the maximum value and minimum value out of all values of the array.

Let’s take an example to understand the problem,

Input

arr[] = {2, 1, 6, 9, 4, 10, 15, 21}

Output

max = 21 , min = 1

Solution Approach

There can be multiple solutions to the problem,

One solution would be directly comparing elements of the array. This is done by checking if each element of the array and then find the max and min using comparison.

This can be done using two different approaches,

  • Iterative Approach
  • Recursive Approach

Iterative approach to solve the problem,

We will loop for the array, extract each element of the array, and compare it with the max and min element of the array.

Program to illustrate that working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
void getMinMax(int arr[] , int N){
   int max = arr[0], min = arr[0];
   for(int i = 1; i < N; i++){
      if(max < arr[i])
         max = arr[i];
      if(min > arr[i])
         min = arr[i];
   }
   cout<<"Maximum Value = "<<max<<"\n";
   cout<<"Minimum Value = "<<min;
}
int main(){
   int arr[] = {2, 1, 6, 9, 4, 10, 15, 21};
   int N = 8;
   getMinMax(arr, N);
   return 0;
}

Output

Maximum Value = 21
Minimum Value = 1

Recursive approach to solve the problem,

In this approach, we will solve the problem by finding the max and min of all elements of the array by recursively calling the method again and again for all elements of the array.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
int CalcMinValue(int arr[], int n) {
   return (n == 1) ? arr[0] : min(arr[n - 1], CalcMinValue(arr, n - 1));
}
int CalcMaxValue(int arr[], int n) {
   return (n == 1) ? arr[0] : max(arr[n -1], CalcMinValue(arr, n - 1));
}
int main() {
   int arr[] = {2, 1, 6, 9, 4, 10, 15, 21};
   int N = 8;
   cout<<"Maximum Value = "<<CalcMaxValue(arr, N)<<endl;
   cout<<"Minimum Value = "<<CalcMinValue(arr, N);
   return 0;
}

Output

Maximum Value = 21
Minimum Value = 1

This problem can also be solved using the inbuild functions that are provided in the standard template library of the C++ programming language.

The methods to find the solution are min_element() and max_element() and these methods are found in the bits/stdc++.h library in C++.

Program to illustrate the solution to the problem,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   int arr[] = {2, 1, 6, 9, 4, 10, 15, 21};
   int N = 8;
   cout<<"Maximum Value = "<<(*max_element(arr, arr+N))<<endl;
   cout<<"Minimum Value = "<<(*min_element(arr, arr+N));
   return 0;
}

Output

Maximum Value = 21
Minimum Value = 1
raja
Published on 08-Jun-2020 14:15:01
Advertisements