Write a program to find minimum and maximum elements from a list of integers

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.




// CPP program to find minimum (or maximum) element
// in an array.
#include <bits/stdc++.h>
using namespace std;
int getMin(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = min(res, arr[i]);
return res;
}
int getMax(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = max(res, arr[i]);
return res;
}
int main()
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum element of array: " << getMin(arr, n) << "\n";
cout << "Maximum element of array: " << getMax(arr, n);
return 0;
}




// Java program to find minimum (or maximum)
// element in an array.
import java.io.*;
class GFG {
static int getMin(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.min(res, arr[i]);
return res;
}
static int getMax(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.max(res, arr[i]);
return res;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = arr.length;
System.out.println( "Minimum element"
+ " of array: " + getMin(arr, n));
System.out.println( "Maximum element"
+ " of array: " + getMax(arr, n));
}
}
// This code is contributed by anuj_67.




# Python3 program to find minimum
# (or maximum) element in an array
# Minimum Function
def getMin(arr, n):
res = arr[0]
for i in range(1,n):
res = min(res, arr[i])
return res
# Maximum Function
def getMax(arr, n):
res = arr[0]
for i in range(1,n):
res = max(res, arr[i])
return res
# Driver Program
arr = [12, 1234, 45, 67, 1]
n = len(arr)
print ("Minimum element of array:", getMin(arr, n))
print ("Maximum element of array:", getMax(arr, n))
# This code is contributed
# by Shreyanshi Arun.




// C# program to find
// minimum (or maximum)
// element in an array.
using System;
class GFG
{
static int getMin(int []arr,
int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.Min(res, arr[i]);
return res;
}
static int getMax(int []arr,
int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.Max(res, arr[i]);
return res;
}
// Driver code
public static void Main ()
{
int []arr = {12, 1234, 45, 67, 1};
int n = arr.Length;
Console.Write("Minimum element" +
" of array: " +
getMin(arr, n) + "\n" );
Console.Write("Maximum element" +
" of array: " +
getMax(arr, n));
}
}
// This code is contributed by Smita.




<?php
// PHP program to find minimum
// (or maximum) element in an
// array.
function getMin($arr, $n)
{
$res = $arr[0];
for ($i = 1; $i < $n; $i++)
$res = min($res, $arr[$i]);
return $res;
}
function getMax($arr, $n)
{
$res = $arr[0];
for ($i = 1; $i < $n; $i++)
$res = max($res, $arr[$i]);
return $res;
}
// Driver Code
$arr = array(12, 1234, 45, 67, 1);
$n = sizeof($arr);
echo "Minimum element of array: "
, getMin($arr, $n), "\n";
echo "Maximum element of array: "
,getMax($arr, $n);
// This code is contributed by ajit
?>




<script>
// JavaScript program to find minimum
// (or maximum) element in an array.
function getMin(arr, n)
{
let res = arr[0];
for(let i = 1; i < n; i++)
res = Math.min(res, arr[i]);
return res;
}
function getMax(arr, n)
{
let res = arr[0];
for(let i = 1; i < n; i++)
res = Math.max(res, arr[i]);
return res;
}
// Driver Code
let arr = [ 12, 1234, 45, 67, 1 ];
let n = arr.length;
document.write("Minimum element" +
" of array: " +
getMin(arr, n) + "<br/>");
document.write("Maximum element" +
" of array: " +
getMax(arr, n));
// This code is contributed by sanjoy_62
</script>

Output:

Minimum element of array: 1 Maximum element of array: 1234

Time Complexity:O(n)

Recursive Solution




// CPP program to find
// minimum (or maximum) element
// in an array.
#include <bits/stdc++.h>
using namespace std;
int getMin(int arr[], int n)
{
// If there is single element, return it.
// Else return minimum of first element and
// minimum of remaining array.
return (n == 1) ? arr[0] : min(arr[0],
getMin(arr + 1, n - 1));
}
int getMax(int arr[], int n)
{
// If there is single element, return it.
// Else return maximum of first element and
// maximum of remaining array.
return (n == 1) ? arr[0] : max(arr[0],
getMax(arr + 1, n - 1));
}
int main()
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum element of array: " <<
getMin(arr, n) << "\n";
cout << "Maximum element of array: " <<
getMax(arr, n);
return 0;
}




// Java program to find minimum
// (or maximum) element
// in an array.
class GFG
{
static int getMin(int arr[], int i, int n)
{
// If there is single element, return it.
// Else return minimum of first element and
// minimum of remaining array.
return (n == 1) ? arr[i] : Math.min(arr[i],
getMin(arr,i + 1 , n - 1));
}
static int getMax(int arr[], int i, int n)
{
// If there is single element, return it.
// Else return maximum of first element and
// maximum of remaining array.
return (n == 1) ? arr[i] : Math.max(arr[i],
getMax(arr ,i + 1, n - 1));
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = arr.length;
System.out.print("Minimum element of array: " +
getMin(arr, 0, n) + "\n");
System.out.println("Maximum element of array: " +
getMax(arr, 0, n));
}
}
/* This code contributed by PrinciRaj1992 */




# Python3 program to find minimum
# (or maximum) element in an array.
def getMin(arr, n):
if(n==1):
return arr[0]
# If there is single element, return it.
# Else return minimum of first element
# and minimum of remaining array.
else:
return min(getMin(arr[1:], n-1), arr[0])
def getMax(arr, n):
if(n==1):
return arr[0]
# If there is single element, return it.
# Else return maximum of first element
# and maximum of remaining array.
else:
return max(getMax(arr[1:], n-1), arr[0])
# Driver code
arr = [12, 1234, 45, 67, 1]
n = len(arr)
print("Minimum element of array: ",
getMin(arr, n));
print("Maximum element of array: ",
getMax(arr, n));
# This code is contributed by
# Mohit Kumar 29




// C# program to find minimum
// (or maximum) element
// in an array.
using System;
class GFG
{
static int getMin(int []arr, int i, int n)
{
// If there is single element, return it.
// Else return minimum of first element and
// minimum of remaining array.
return (n == 1) ? arr[i] : Math.Min(arr[i],
getMin(arr,i + 1 , n - 1));
}
static int getMax(int []arr, int i, int n)
{
// If there is single element, return it.
// Else return maximum of first element and
// maximum of remaining array.
return (n == 1) ? arr[i] : Math.Max(arr[i],
getMax(arr ,i + 1, n - 1));
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 12, 1234, 45, 67, 1 };
int n = arr.Length;
Console.WriteLine("Minimum element of array: " +
getMin(arr, 0, n));
Console.WriteLine("Maximum element of array: " +
getMax(arr, 0, n));
}
}
// This code is contribute by Mohit




<script>
// javascript program to find minimum
// (or maximum) element
// in an array.
function getMin(arr , i , n) {
// If there is single element, return it.
// Else return minimum of first element and
// minimum of remaining array.
return (n == 1) ? arr[i] : Math.min(arr[i], getMin(arr, i + 1, n - 1));
}
function getMax(arr , i , n) {
// If there is single element, return it.
// Else return maximum of first element and
// maximum of remaining array.
return (n == 1) ? arr[i] : Math.max(arr[i], getMax(arr, i + 1, n - 1));
}
// Driver code
var arr = [ 12, 1234, 45, 67, 1 ];
var n = arr.length;
document.write("Minimum element of array: " + getMin(arr, 0, n) + "<br/>");
document.write("Maximum element of array: " + getMax(arr, 0, n));
// This code contributed by Rajput-Ji
</script>

Output:

Min of array: 1 Max of array: 1234

Using Library functions:
We can use min_element() and max_element() to find minimum and maximum of array.




// CPP program to find minimum (or maximum) element
// in an array.
#include <bits/stdc++.h>
using namespace std;
int getMin(int arr[], int n)
{
return *min_element(arr, arr + n);
}
int getMax(int arr[], int n)
{
return *max_element(arr, arr + n);
}
int main()
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum element of array: " << getMin(arr, n) << "\n";
cout << "Maximum element of array: " << getMax(arr, n);
return 0;
}




import java.util.Arrays;
// Java program to find minimum (or maximum) element
// in an array.
import java.util.Arrays;
class GFG {
static int getMin(int arr[], int n) {
return Arrays.stream(arr).min().getAsInt();
}
static int getMax(int arr[], int n) {
return Arrays.stream(arr).max().getAsInt();
}
// Driver code
public static void main(String[] args) {
int arr[] = {12, 1234, 45, 67, 1};
int n = arr.length;
System.out.println("Minimum element of array: " + getMin(arr, n));
System.out.println("Maximum element of array: " + getMax(arr, n));
}
}
/*This Java code is contributed by 29AjayKumar*/




# Python3 program to find minimum
# (or maximum) element
# in an array.
def getMin(arr,n):
return min(arr)
def getMax(arr,n):
return max(arr)
# Driver Code
if __name__=='__main__':
arr = [12,1234,45,67,1]
n = len(arr)
print("Minimum element of array: "
,getMin(arr, n))
print("Maximum element of array: "
,getMax(arr, n))
# This code is contributed by
# Shrikant13




// C# program to find minimum
// (or maximum) element in an array.
using System;
using System.Linq;
class GFG
{
static int getMin(int []arr, int n)
{
return arr.Min();
}
static int getMax(int []arr, int n)
{
return arr.Max();
}
// Driver code
public static void Main(String[] args)
{
int []arr = {12, 1234, 45, 67, 1};
int n = arr.Length;
Console.WriteLine("Minimum element of array: " +
getMin(arr, n));
Console.WriteLine("Maximum element of array: " +
getMax(arr, n));
}
}
// This code is contributed by 29AjayKumar




<?php
// PHP program to find minimum (or maximum)
// element in an array.
function getMin(&$arr, $n)
{
return min($arr);
}
function getMax(&$arr, $n)
{
return max($arr);
}
// Driver Code
$arr = array(12, 1234, 45, 67, 1 );
$n = sizeof($arr);
echo "Minimum element of array: " .
getMin($arr, $n) . "\n";
echo "Maximum element of array: " .
getMax($arr, $n);
// This code is contributed
// by ChitraNayal
?>




<script>
// Javascript program to find
// minimum (or maximum) element
// in an array.
function getMin(arr , n)
{
return Math.min.apply(Math,arr);
}
function getMax(arr , n) {
return Math.max.apply(Math,arr);
}
// Driver code
var arr = [ 12, 1234, 45, 67, 1 ];
var n = arr.length;
document.write("Minimum element of array: " +
getMin(arr, n)+"<br/>");
document.write("Maximum element of array: "
+ getMax(arr, n));
// This code contributed by aashish2995
</script>

Output:

Minimum element of array: 1 Maximum element of array: 1234

Write a program to find minimum and maximum elements from a list of integers




Article Tags :
Recursion
School Programming
Searching
CBSE - Class 11
school-programming
Practice Tags :
Searching
Recursion

Maximum and minimum of an array using minimum number of comparisons

Write a C function to return minimum and maximum in an array. Your program should make the minimum number of comparisons.

Python to Find Minimum and Maximum Elements of List

  • Python Program to find the Largest and Smallest Number in a List using min() and max() method.
  • Python Program to find the Minimum and Maximum Number in a List with their position using for loop and if statement.
  • Python Program to find the Largest and Smallest Number in a List using sort() method.
  • Python Program to find the position of min and max elements of a list using min() and max() function.

1: Python Program to find the Largest and Smallest Number in a List using min() and max() method

  • Allow user to enter the length of the list.
  • Next, iterate the for loop and add the number in the list.
  • Use theminandmaxfunctions to find the smallest and largest numbers from the list.
  • Print the results.
# Python Program to find Largest and Smallest Number in a List NumList = [] Number = int(input("Please enter element length in list ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) print("The Smallest Element in this List is : ", min(NumList)) print("The Largest Element in this List is : ", max(NumList))

After executing the program, the output will be:

How many element in list, Please enter num :- 5 Please enter the Value of 1 Element : 500 Please enter the Value of 2 Element : 653 Please enter the Value of 3 Element : 895 Please enter the Value of 4 Element : 565 Please enter the Value of 5 Element : 568 The Smallest Element in this List is : 500 The Largest Element in this List is : 895
Recommended:-Python Program to Find Smallest/Minimum of n Numbers

2: Python Program to find the Minimum and Maximum Number in a List with their position using for loop and if statement

  • Allow user to enter the length of the list.
  • Next, iterate the for loop and add the number in the list.
  • Iterate for loop with list and use if statement to find the min and max number and their position in the list.
  • Print the results.
# Python Program to find Largest and Smallest Number in a List # Python Program to find Largest and Smallest Number in a List NumList = [] Number = int(input("How many element in list, Please enter num :- ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) smallest = largest = NumList[0] for j in range(1, Number): if(smallest > NumList[j]): smallest = NumList[j] min_position = j if(largest < NumList[j]): largest = NumList[j] max_position = j print("The Smallest Element in this List is : ", smallest) print("The Index position of Smallest Element in this List is : ", min_position) print("The Largest Element in this List is : ", largest) print("The Index position of Largest Element in this List is : ", max_position)

After executing the program, the output will be:

How many element in list, Please enter num :- 5 Please enter the Value of 1 Element : 70 Please enter the Value of 2 Element : 90 Please enter the Value of 3 Element : 60 Please enter the Value of 4 Element : 80 Please enter the Value of 5 Element : 45 The Smallest Element in this List is : 45 The Index position of Smallest Element in this List is : 4 The Largest Element in this List is : 90 The Index position of Largest Element in this List is : 1

3: Python Program to find the Largest and Smallest Number in a List using sort() method

  • Allow user to enter the length of the list.
  • Next, iterate the for loop and add the number in the list.
  • Use python sort method to find the smallest and largest number from the list.
  • Print the results.
# Python Program to find Largest and Smallest Number in a List NumList = [] Number = int(input("How many element in list, please enter num :- ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) NumList.sort() print("The Smallest Element in this List is : ", NumList[0]) print("The Largest Element in this List is : ", NumList[Number - 1])

After executing the program, the output will be:

How many element in list, please enter num :- 5 Please enter the Value of 1 Element : 98 Please enter the Value of 2 Element : 45 Please enter the Value of 3 Element : 63 Please enter the Value of 4 Element : 78 Please enter the Value of 5 Element : 25 The Smallest Element in this List is : 25 The Largest Element in this List is : 98

4: Python Program to find the position of min and max elements of a list using min() and max() function

  • Allow user to enter the length of the list.
  • Next, iterate the for loop and add the number in the list.
  • Use min and max function with index function to find the position of an element in the list.
  • Print the results.
# Python Program to find Largest and Smallest Number in a List NumList = [] Number = int(input("How many element in list, Please enter num :- ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) # min element's position/index min = NumList.index(min(NumList)) # max element's position/index max = NumList.index(max(NumList)) # printing the position/index of min and max elements print("position of minimum element: ", min) print("position of maximum element: ", max)

After executing the program, the output will be:

How many element in list, Please enter num :- 5 Please enter the Value of 1 Element : 5 Please enter the Value of 2 Element : 8 Please enter the Value of 3 Element : 9 Please enter the Value of 4 Element : 4 Please enter the Value of 5 Element : 2 position of minimum element: 4 position of maximum element: 2

C Program To Find Maximum & Minimum Element In Array | C Prorams

in C Programs Comments Off on C Program To Find Maximum & Minimum Element In Array | C Prorams

C program to find the maximum and minimum element in an array – In this article, we will brief in on the many ways to find the maximum and minimum element in an array in C programming.

Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.

The ways used in this piece are as follows:

  • Using Standard Method
  • Using Function
  • Using Recursion

As we all know, arrays are a collection of a bunch of elements in a sequential pattern in a horizontal direction. Arrays form a very important of C programming.

Write a program to find minimum and maximum elements from a list of integers

As given in the example above, firstly, enter the size of the array that you want to define.

The size of the array in the example mentioned is 5.

After that, you need to enter the elements of the array.

The elements entered in the array are as follows:

1 2 35 0 -1

So, the minimum of the array is -1 and the maximum of the array is 35.

Thus, doing the same using multiple methods in C programming is as follows:

C++


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
using namespace std;
// Naive solution to find the minimum and maximum number in an array
void findMinAndMax(vector<int> const &nums, int &min, int &max)
{
// initialize minimum and maximum element with the first element
max = nums[0], min = nums[0];
// do for each array element
for (int i = 1; i < nums.size(); i++)
{
// if the current element is greater than the maximum found so far
if (nums[i] > max) {
max = nums[i];
}
// if the current element is smaller than the minimum found so far
else if (nums[i] < min) {
min = nums[i];
}
}
}
int main()
{
vector<int> nums = { 5, 7, 2, 4, 9, 6 };
// to store minimum and maximum element, respectively
int min, max;
findMinAndMax(nums, min, max);
cout << "The minimum array element is " << min << endl;
cout << "The maximum array element is " << max << endl;
return 0;
}

DownloadRun Code

C Exercises: Find the maximum and minimum element in an array

Last update on December 18 2021 11:49:16 (UTC/GMT +8 hours)