- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find the minimum (or maximum) element of an array in C++
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
#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
#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
#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
- Related Articles
- C# program to find maximum and minimum element in an array
- How to find the minimum and maximum element of an Array using STL in C++?
- PHP program to find the minimum element in an array
- PHP program to find the maximum element in an array
- C++ Program to Find Minimum Element in an Array using Linear Search
- C++ Program to Find the Minimum element of an Array using Binary Search approach
- C++ program to rearrange an array in maximum minimum form
- C++ Program to Find Maximum Element in an Array using Binary Search
- C Program to Minimum and Maximum prime numbers in an array
- How to find the maximum element of an Array using STL in C++?
- Program to find maximum XOR with an element from array in Python
- C++ Program to Find Largest Element of an Array
- Write a Golang program to find the element with the minimum value in an array
- Maximum and minimum of an array using minimum number of comparisons in C
- C# program to find the last matching element in an array
