Program to find the minimum (or maximum) element of an array
Last Updated :
10 Apr, 2025
Given an array, write functions to find the minimum and maximum elements in it.
Examples:
Input: arr[] = [1, 423, 6, 46, 34, 23, 13, 53, 4]
Output: Minimum element of array: 1
Maximum element of array: 423
Input: arr[] = [2, 4, 6, 7, 9, 8, 3, 11]
Output: Minimum element of array: 2
Maximum element of array: 11
Approach: Using inbuilt sort() function – O(n logn) time and O(1) space
The simplest and most efficient way to find the minimum and maximum values in a collection is by sorting the collection. Many programming languages provide an inbuilt s
ort() function that sorts collections (like arrays, lists, etc.) in ascending order. Once the collection is sorted, the minimum value is at the 0th position, and the maximum value is at the nth position (where n
is the size of the collection).
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
// Implemented inbuilt function to sort the vector
sort(arr.begin(), arr.end());
// After sorting, the value at the 0th position will be the minimum
// and the last position will be the maximum
cout << "Minimum element of array: " << arr[0] << endl;
cout<< "Maximum element of array: " << arr[arr.size() - 1] << endl;
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int main() {
int arr[] = {1, 423, 6, 46, 34, 23, 13, 53, 4};
int n = sizeof(arr) / sizeof(arr[0]);
// Implemented inbuilt function to sort the array
qsort(arr, n, sizeof(int), compare);
// After sorting, the value at the 0th position will be the minimum
// and the last position will be the maximum
printf("Minimum element of array: %d\n", arr[0]);
printf("Maximum element of array: %d\n", arr[n - 1]);
return 0;
}
Java
import java.util.Arrays;
public class GfG {
public static void main(String[] args) {
int[] arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
// Implemented inbuilt function to sort the array
Arrays.sort(arr);
// After sorting, the value at the 0th position will be the minimum
// and the last position will be the maximum
System.out.println("Minimum element of array: " + arr[0]);
System.out.println("Maximum element of array: " + arr[arr.length - 1]);
}
}
Python
arr = [1, 423, 6, 46, 34, 23, 13, 53, 4]
# Implemented inbuilt function to sort the list
arr.sort()
# After sorting, the value at the 0th position will be the minimum
# and the last position will be the maximum
print("Minimum element of array:", arr[0])
print("Maximum element of array:", arr[-1])
C#
using System;
using System.Linq;
class GfG {
static void Main() {
int[] arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
// Implemented inbuilt function to sort the array
Array.Sort(arr);
// After sorting, the value at the 0th position will be the minimum
// and the last position will be the maximum
Console.WriteLine("Minimum element of array: " + arr[0]);
Console.WriteLine("Maximum element of array: " + arr[arr.Length - 1]);
}
}
JavaScript
const arr = [1, 423, 6, 46, 34, 23, 13, 53, 4];
// Implemented inbuilt function to sort the array
arr.sort((x, y) => x - y);
// After sorting, the value at the 0th position will be the minimum
// and the last position will be the maximum
console.log("Minimum element of array:", arr[0]);
console.log("Maximum element of array:", arr[arr.length - 1]);
OutputMinimum element of array: 1
Maximum element of array: 423
Approach: Linear Scan Method – O(n) time and O(1) space
The approach used here is a linear scan to find the minimum and maximum values in a collection, specifically in a vector
in this case. We start by assuming the first element of the collection is both the minimum and the maximum. Then, as we iterate through the collection, we compare each element to the current minimum and maximum. If an element is smaller than the current minimum, we update the minimum; if it’s larger than the current maximum, we update the maximum. This continues for all elements in the collection, and at the end of the loop, we have the minimum and maximum values.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int getMin(const vector<int>& arr)
{
int res = arr[0];
for (int i = 1; i < arr.size(); i++)
res = min(res, arr[i]);
return res;
}
int getMax(const vector<int>& arr)
{
int res = arr[0];
for (int i = 1; i < arr.size(); i++)
res = max(res, arr[i]);
return res;
}
int main()
{
vector<int> arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
cout << "Minimum element of array: " << getMin(arr) << "\n";
cout << "Maximum element of array: " << getMax(arr) << "\n";
return 0;
}
C
#include <stdio.h>
#include <limits.h>
int getMin(int arr[], int size) {
int res = arr[0];
for (int i = 1; i < size; i++)
if (arr[i] < res) res = arr[i];
return res;
}
int getMax(int arr[], int size) {
int res = arr[0];
for (int i = 1; i < size; i++)
if (arr[i] > res) res = arr[i];
return res;
}
int main() {
int arr[] = {1, 423, 6, 46, 34, 23, 13, 53, 4};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Minimum element of array: %d\n", getMin(arr, size));
printf("Maximum element of array: %d\n", getMax(arr, size));
return 0;
}
Java
import java.util.Arrays;
public class GfG {
public static int getMin(int[] arr) {
int res = arr[0];
for (int i = 1; i < arr.length; i++)
res = Math.min(res, arr[i]);
return res;
}
public static int getMax(int[] arr) {
int res = arr[0];
for (int i = 1; i < arr.length; i++)
res = Math.max(res, arr[i]);
return res;
}
public static void main(String[] args) {
int[] arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
System.out.println("Minimum element of array: " + getMin(arr));
System.out.println("Maximum element of array: " + getMax(arr));
}
}
Python
def get_min(arr):
res = arr[0]
for i in arr[1:]:
res = min(res, i)
return res
def get_max(arr):
res = arr[0]
for i in arr[1:]:
res = max(res, i)
return res
arr = [1, 423, 6, 46, 34, 23, 13, 53, 4]
print("Minimum element of array:", get_min(arr))
print("Maximum element of array:", get_max(arr))
C#
using System;
class GfG {
static int GetMin(int[] arr) {
int res = arr[0];
for (int i = 1; i < arr.Length; i++)
res = Math.Min(res, arr[i]);
return res;
}
static int GetMax(int[] arr) {
int res = arr[0];
for (int i = 1; i < arr.Length; i++)
res = Math.Max(res, arr[i]);
return res;
}
static void Main() {
int[] arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
Console.WriteLine("Minimum element of array: " + GetMin(arr));
Console.WriteLine("Maximum element of array: " + GetMax(arr));
}
}
JavaScript
function getMin(arr) {
let res = arr[0];
for (let i = 1; i < arr.length; i++)
res = Math.min(res, arr[i]);
return res;
}
function getMax(arr) {
let res = arr[0];
for (let i = 1; i < arr.length; i++)
res = Math.max(res, arr[i]);
return res;
}
let arr = [1, 423, 6, 46, 34, 23, 13, 53, 4];
console.log("Minimum element of array: " + getMin(arr));
console.log("Maximum element of array: " + getMax(arr));
OutputMinimum element of array: 1
Maximum element of array: 423
Approach: Recursive Approach – O(n) time and O(n) space
The approach used in the code is a recursive solution to find the minimum and maximum values in a collection (in this case, a vector).
Here’s how it works:
1. Base Case: For each function (getMin
and getMax
), the base case is when there’s only one element left in the vector (n == 1
). In this case, the function simply returns that element because it’s both the minimum and maximum.
2. Recursive Step: If there are more than one element, the function compares the current element (arr[n-1]
) with the result of the recursive call on the rest of the vector (getMin(arr, n-1)
or getMax(arr, n-1)
):
- For
getMin
: The function returns the smaller of the current element (arr[n-1]
) and the result of the recursive call, which finds the minimum in the rest of the array. - For
getMax
: Similarly, it returns the larger of the current element and the result of the recursive call, which finds the maximum in the remaining array.
3. End Result: The recursion continues until only one element remains, and at each recursive step, the function builds up the final minimum or maximum value by comparing and returning the smallest or largest values found so far.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int getMin(const vector<int>& arr, int n)
{
// If there is a single element, return it.
// Else, return the minimum of the first element and the minimum of the remaining array.
if (n == 1) {
return arr[0];
}
return min(arr[n - 1], getMin(arr, n - 1));
}
int getMax(const vector<int>& arr, int n)
{
// If there is a single element, return it.
// Else, return the maximum of the first element and the maximum of the remaining array.
if (n == 1) {
return arr[0];
}
return max(arr[n - 1], getMax(arr, n - 1));
}
int main()
{
vector<int> arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
int n = arr.size();
cout << "Minimum element of array: " << getMin(arr, n) << "\n";
cout << "Maximum element of array: " << getMax(arr, n) << "\n";
return 0;
}
C
#include <stdio.h>
#include <limits.h>
int getMin(int arr[], int n) {
// If there is a single element, return it.
// Else, return the minimum of the first element and the minimum of the remaining array.
if (n == 1) {
return arr[0];
}
return (arr[n - 1] < getMin(arr, n - 1)) ? arr[n - 1] : getMin(arr, n - 1);
}
int getMax(int arr[], int n) {
// If there is a single element, return it.
// Else, return the maximum of the first element and the maximum of the remaining array.
if (n == 1) {
return arr[0];
}
return (arr[n - 1] > getMax(arr, n - 1)) ? arr[n - 1] : getMax(arr, n - 1);
}
int main() {
int arr[] = {1, 423, 6, 46, 34, 23, 13, 53, 4};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Minimum element of array: %d\n", getMin(arr, n));
printf("Maximum element of array: %d\n", getMax(arr, n));
return 0;
}
Java
import java.util.Arrays;
public class MinMax {
public static int getMin(int[] arr, int n) {
// If there is a single element, return it.
// Else, return the minimum of the first element and the minimum of the remaining array.
if (n == 1) {
return arr[0];
}
return Math.min(arr[n - 1], getMin(arr, n - 1));
}
public static int getMax(int[] arr, int n) {
// If there is a single element, return it.
// Else, return the maximum of the first element and the maximum of the remaining array.
if (n == 1) {
return arr[0];
}
return Math.max(arr[n - 1], getMax(arr, n - 1));
}
public static void main(String[] args) {
int[] arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
int n = arr.length;
System.out.println("Minimum element of array: " + getMin(arr, n));
System.out.println("Maximum element of array: " + getMax(arr, n));
}
}
Python
def getMin(arr, n):
# If there is a single element, return it.
# Else, return the minimum of the first element and the minimum of the remaining array.
if n == 1:
return arr[0]
return min(arr[n - 1], getMin(arr, n - 1))
def getMax(arr, n):
# If there is a single element, return it.
# Else, return the maximum of the first element and the maximum of the remaining array.
if n == 1:
return arr[0]
return max(arr[n - 1], getMax(arr, n - 1))
arr = [1, 423, 6, 46, 34, 23, 13, 53, 4]
n = len(arr)
print("Minimum element of array:", getMin(arr, n))
print("Maximum element of array:", getMax(arr, n))
C#
using System;
class MinMax {
public static int GetMin(int[] arr, int n) {
// If there is a single element, return it.
// Else, return the minimum of the first element and the minimum of the remaining array.
if (n == 1) {
return arr[0];
}
return Math.Min(arr[n - 1], GetMin(arr, n - 1));
}
public static int GetMax(int[] arr, int n) {
// If there is a single element, return it.
// Else, return the maximum of the first element and the maximum of the remaining array.
if (n == 1) {
return arr[0];
}
return Math.Max(arr[n - 1], GetMax(arr, n - 1));
}
public static void Main() {
int[] arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
int n = arr.Length;
Console.WriteLine("Minimum element of array: " + GetMin(arr, n));
Console.WriteLine("Maximum element of array: " + GetMax(arr, n));
}
}
JavaScript
function getMin(arr, n) {
// If there is a single element, return it.
// Else, return the minimum of the first element and the minimum of the remaining array.
if (n === 1) {
return arr[0];
}
return Math.min(arr[n - 1], getMin(arr, n - 1));
}
function getMax(arr, n) {
// If there is a single element, return it.
// Else, return the maximum of the first element and the maximum of the remaining array.
if (n === 1) {
return arr[0];
}
return Math.max(arr[n - 1], getMax(arr, n - 1));
}
const arr = [1, 423, 6, 46, 34, 23, 13, 53, 4];
const n = arr.length;
console.log("Minimum element of array: " + getMin(arr, n));
console.log("Maximum element of array: " + getMax(arr, n));
OutputMinimum element of array: 1
Maximum element of array: 423
Approach: Using Inbuilt Functions – O(n) time and O(1) space
The approach involves using inbuilt functions to find the minimum and maximum elements in a collection, such as an array or a vector. These functions work by scanning through the entire collection and comparing each element to determine the smallest or largest value.
- Minimum element: The function compares each element and returns the smallest one.
- Maximum element: The function compares each element and returns the largest one.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int getMin(const vector<int>& arr)
{
return *min_element(arr.begin(), arr.end());
}
int getMax(const vector<int>& arr)
{
return *max_element(arr.begin(), arr.end());
}
int main()
{
vector<int> arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
// Find and print the minimum and maximum elements
cout << "Minimum element of array: " << getMin(arr) << "\n";
cout << "Maximum element of array: " << getMax(arr) << "\n";
return 0;
}
C
#include <stdio.h>
#include <limits.h>
int getMin(int arr[], int size) {
int min = INT_MAX;
for (int i = 0; i < size; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
int getMax(int arr[], int size) {
int max = INT_MIN;
for (int i = 0; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
int main() {
int arr[] = {1, 423, 6, 46, 34, 23, 13, 53, 4};
int size = sizeof(arr) / sizeof(arr[0]);
// Find and print the minimum and maximum elements
printf("Minimum element of array: %d\n", getMin(arr, size));
printf("Maximum element of array: %d\n", getMax(arr, size));
return 0;
}
Java
import java.util.Arrays;
public class Main {
public static int getMin(int[] arr) {
return Arrays.stream(arr).min().getAsInt();
}
public static int getMax(int[] arr) {
return Arrays.stream(arr).max().getAsInt();
}
public static void main(String[] args) {
int[] arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
// Find and print the minimum and maximum elements
System.out.println("Minimum element of array: " + getMin(arr));
System.out.println("Maximum element of array: " + getMax(arr));
}
}
Python
def getMin(arr):
return min(arr)
def getMax(arr):
return max(arr)
arr = [1, 423, 6, 46, 34, 23, 13, 53, 4]
# Find and print the minimum and maximum elements
print("Minimum element of array:", getMin(arr))
print("Maximum element of array:", getMax(arr))
C#
using System;
using System.Linq;
class GfG {
static int GetMin(int[] arr) {
return arr.Min();
}
static int GetMax(int[] arr) {
return arr.Max();
}
static void Main() {
int[] arr = {1, 423, 6, 46, 34, 23, 13, 53, 4};
// Find and print the minimum and maximum elements
Console.WriteLine("Minimum element of array: " + GetMin(arr));
Console.WriteLine("Maximum element of array: " + GetMax(arr));
}
}
JavaScript
function getMin(arr) {
return Math.min(...arr);
}
function getMax(arr) {
return Math.max(...arr);
}
const arr = [1, 423, 6, 46, 34, 23, 13, 53, 4];
// Find and print the minimum and maximum elements
console.log("Minimum element of array:", getMin(arr));
console.log("Maximum element of array:", getMax(arr));
OutputMinimum element of array: 1
Maximum element of array: 423
Similar Reads
Find the minimum and maximum sum of N-1 elements of the array
Given an unsorted array A of size N, the task is to find the minimum and maximum values that can be calculated by adding exactly N-1 elements. Examples: Input: a[] = {13, 5, 11, 9, 7} Output: 32 40 Explanation: Minimum sum is 5 + 7 + 9 + 11 = 32 and maximum sum is 7 + 9 + 11 + 13 = 40.Input: a[] = {
10 min read
Sum and Product of minimum and maximum element of an Array
Given an array. The task is to find the sum and product of the maximum and minimum elements of the given array. Examples: Input : arr[] = {12, 1234, 45, 67, 1} Output : Sum = 1235 Product = 1234 Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9} Output : Sum = 10 Product = 9 Take two variables min and max to
13 min read
Leftmost and rightmost indices of the maximum and the minimum element of an array
Given an array arr[], the task is to find the leftmost and the rightmost indices of the minimum and the maximum element from the array where arr[] consists of non-distinct elements.Examples: Input: arr[] = {2, 1, 1, 2, 1, 5, 6, 5} Output: Minimum left : 1 Minimum right : 4 Maximum left : 6 Maximum r
15+ min read
Program to find largest element in an Array
Given an array arr. The task is to find the largest element in the given array. Examples: Input: arr[] = [10, 20, 4]Output: 20Explanation: Among 10, 20 and 4, 20 is the largest. Input: arr[] = [20, 10, 20, 4, 100]Output: 100 Table of Content Iterative Approach - O(n) Time and O(1) SpaceRecursive App
7 min read
Maximize minimum element of an Array using operations
Given an array A[] of N integers and two integers X and Y (X ⤠Y), the task is to find the maximum possible value of the minimum element in an array A[] of N integers by adding X to one element and subtracting Y from another element any number of times, where X ⤠Y. Examples: Input: N= 3, A[] = {1,
9 min read
Javascript Program to Find k maximum elements of array in original order
Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input. Note: k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their app
4 min read
Minimum index of element with maximum multiples in Array
Given an array arr[] of length n, the task is to calculate the min index of the element which has maximum elements present in the form of {2 * arr[i], 3 * arr[i], 4 * arr[i], 5 * arr[i]}. Examples: Input: n = 4. arr[] = {2, 1, 3, 6}Output: 1Explanation: For 2, {2*2, 3*2, 4*2, 5*2} => {4, 6, 8, 10
5 min read
Maximize count of elements reaching the end of an Array
Given an array arr[] consisting of N integers, where each element denotes the maximum number of elements that can be placed on that index and an integer X, which denotes the maximum indices that can be jumped from an index, the task is to find the number of elements that can reach the end of the arr
15 min read
Type of array and its maximum element
Given an array, it can be of 4 types. Ascending Descending Ascending Rotated Descending Rotated Find out which kind of array it is and return the maximum of that array. Examples: Input : arr[] = { 2, 1, 5, 4, 3} Output : Descending rotated with maximum element 5 Input : arr[] = { 3, 4, 5, 1, 2} Outp
15+ min read
Rearrange an array in maximum minimum form in O(1) extra space
Given a sorted array of positive integers, rearrange the array alternately i.e first element should be the maximum value, second minimum value, third-second max, fourth-second min and so on. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2, 5, 3, 4}Explanation: First 7 is t
9 min read