Given an array of integers arr[]. Find the minimum element.
Examples:
Input: arr[] = [1, 2, 3, 4]
Output: 1
Explanation: The smallest element in the array is 1.Input: arr[] = [3, 2, 1]
Output: 1
Explanation: The smallest element in the array is 1.Input: arr[] = [5, 4, 2, 8, 8]
Output: 2
Explanation: The smallest element in the array is 2.
Try It Yourself
Table of Content
Iterative Approach - O(n) Time and O(1) Space
The idea is to traverse the array and keep track of the smallest element found so far.
Working of Approach:
- Initialize mn with the first element of the array.
- Traverse the array from the second element.
- Compare each element with mn.
- Update mn if the current element is smaller.
- Return mn after completing the traversal.
#include
#include
using namespace std;
int findMin(vector<int> &arr)
{
// Initialize minimum with the first element
int mn = arr[0];
// Traverse the remaining elements
for (int i = 1; i < arr.size(); i++)
{
// Update minimum if a smaller element is found
if (arr[i] < mn)
mn = arr[i];
}
return mn;
}
int main()
{
vector<int> arr = {5, 4, 2, 8, 8};
cout << findMin(arr);
return 0;
}
import java.util.Arrays;
public class GFG {
// Initialize minimum with the first element
static int findMin(int[] arr)
{
int mn = arr[0];
// Traverse the remaining elements
for (int i = 1; i < arr.length; i++) {
// Update minimum if a smaller element is found
if (arr[i] < mn)
mn = arr[i];
}
return mn;
}
public static void main(String[] args)
{
int[] arr = { 5, 4, 2, 8, 8 };
System.out.println(findMin(arr));
}
}
def findMin(arr):
# Initialize minimum with the first element
mn = arr[0]
# Traverse the remaining elements
for i in range(1, len(arr)):
# Update minimum if a smaller element is found
if arr[i] < mn:
mn = arr[i]
return mn
if __name__ == "__main__":
arr = [5, 4, 2, 8, 8]
print(findMin(arr))
using System;
public class GFG {
// Initialize minimum with the first element
public static int findMin(int[] arr)
{
int mn = arr[0];
// Traverse the remaining elements
for (int i = 1; i < arr.Length; i++) {
// Update minimum if a smaller element is found
if (arr[i] < mn)
mn = arr[i];
}
return mn;
}
public static void Main()
{
int[] arr = { 5, 4, 2, 8, 8 };
Console.WriteLine(findMin(arr));
}
}
function findMin(arr)
{
// Initialize minimum with the first element
let mn = arr[0];
// Traverse the remaining elements
for (let i = 1; i < arr.length; i++) {
// Update minimum if a smaller element is found
if (arr[i] < mn)
mn = arr[i];
}
return mn;
}
// Driver Code
const arr = [ 5, 4, 2, 8, 8 ];
console.log(findMin(arr));
Output
2
Recursive Approach - O(n) Time and O(n) Space
The idea is to recursively find the minimum in the remaining part of the array and compare it with the current element.
Working of Approach:
- Start from the first element of the array.
- Recursively find the minimum from the next index onward.
- Use the last element as the base case.
- Compare the current element with the recursive result.
- Return the smaller of the two values.
#include
#include
#include
using namespace std;
// Recursive function to find the minimum element
int findMinRec(vector<int> &arr, int i)
{
// Base case: last element
if (i == arr.size() - 1)
return arr[i];
// Find minimum in the remaining array
int mn = findMinRec(arr, i + 1);
// Return the smaller element
return min(arr[i], mn);
}
int findMin(vector<int> &arr)
{
return findMinRec(arr, 0);
}
int main()
{
vector<int> arr = {5, 4, 2, 8, 8};
cout << findMin(arr);
return 0;
}
import java.util.Arrays;
public class GFG {
// Recursive function to find the minimum element
public static int findMinRec(int[] arr, int i)
{
// Base case: last element
if (i == arr.length - 1)
return arr[i];
// Find minimum in the remaining array
int mn = findMinRec(arr, i + 1);
// Return the smaller element
return Math.min(arr[i], mn);
}
public static int findMin(int[] arr)
{
return findMinRec(arr, 0);
}
public static void main(String[] args)
{
int[] arr = { 5, 4, 2, 8, 8 };
System.out.println(findMin(arr));
}
}
def findMinRec(arr, i):
# Base case: last element
if i == len(arr) - 1:
return arr[i]
# Find minimum in the remaining array
mn = findMinRec(arr, i + 1)
# Return the smaller element
return min(arr[i], mn)
def findMin(arr):
return findMinRec(arr, 0)
if __name__ == '__main__':
arr = [5, 4, 2, 8, 8]
print(findMin(arr))
using System;
public class GFG {
// Recursive function to find the minimum element
public static int findMinRec(int[] arr, int i)
{
// Base case: last element
if (i == arr.Length - 1)
return arr[i];
// Find minimum in the remaining array
int mn = findMinRec(arr, i + 1);
// Return the smaller element
return Math.Min(arr[i], mn);
}
public static int findMin(int[] arr)
{
return findMinRec(arr, 0);
}
public static void Main()
{
int[] arr = { 5, 4, 2, 8, 8 };
Console.WriteLine(findMin(arr));
}
}
/* Recursive function to find the minimum element */
function findMinRec(arr, i)
{
// Base case: last element
if (i === arr.length - 1)
return arr[i];
// Find minimum in the remaining array
let mn = findMinRec(arr, i + 1);
// Return the smaller element
return Math.min(arr[i], mn);
}
function findMin(arr) { return findMinRec(arr, 0); }
// Driver Code
let arr = [ 5, 4, 2, 8, 8 ];
console.log(findMin(arr));
Output
2
Library Methods - O(n) Time and O(1) Space
The idea is to use the language's inbuilt function to directly find the smallest element in the array.
Working of Approach:
- Use the language's inbuilt function to find the minimum element.
- Pass the array to the function.
- The function checks the elements and finds the smallest value.
- Store or return the obtained minimum element.
- Return the minimum value as the answer.
#include
#include
#include
using namespace std;
int findMin(vector<int> &arr)
{
// Find the iterator pointing to the minimum element
auto it = min_element(arr.begin(), arr.end());
// Return the minimum element
return *it;
}
int main()
{
vector<int> arr = {5, 4, 2, 8, 8};
cout << findMin(arr);
return 0;
}
import java.util.*;
class GFG {
static int findMin(int[] arr)
{
// Find the minimum element
int min = Arrays.stream(arr).min().getAsInt();
// Return the minimum element
return min;
}
public static void main(String[] args)
{
int[] arr = { 5, 4, 2, 8, 8 };
System.out.println(findMin(arr));
}
}
def findMin(arr):
# Find the minimum element
return min(arr)
if __name__ == '__main__':
arr = [5, 4, 2, 8, 8]
print(findMin(arr))
using System;
using System.Linq;
class GFG {
static int findMin(int[] arr)
{
// Find the minimum element
int min = arr.Min();
// Return the minimum element
return min;
}
public static void Main()
{
int[] arr = { 5, 4, 2, 8, 8 };
Console.WriteLine(findMin(arr));
}
}
function findMin(arr)
{
// Find the minimum element
return Math.min(...arr);
}
// Driver Code
let arr = [ 5, 4, 2, 8, 8 ];
console.log(findMin(arr));
Output
2