Program to find the minimum (or maximum) element of an array
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: 423Input: 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 (wheren
is the size of the collection).
#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;
}
#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;
}
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]);
}
}
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])
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]);
}
}
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]);
Output
Minimum 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.
#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;
}
#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;
}
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));
}
}
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))
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));
}
}
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));
Output
Minimum 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.
#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;
}
#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;
}
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));
}
}
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))
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));
}
}
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));
Output
Minimum 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.
#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;
}
#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;
}
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));
}
}
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))
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));
}
}
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));
Output
Minimum element of array: 1 Maximum element of array: 423