# How To Find Minimum and Maximum Element in an Array (C, CPP & Python)

Find Minimum and Maximum Element in an Array When working with arrays, it’s common to need to find the min and max values contained within the array. In this article, we’ll explore several different methods for finding the min and max values in an array using various programming languages.

## Understanding Arrays

Before we dive into how to find min and max values in an array, let’s first take a moment to understand what an array is. An array is a collection of data elements of the same type that are stored together in contiguous memory locations. These data elements can be accessed using an index or a key.

Arrays are widely used in programming for storing and manipulating data. They are a fundamental data structure and are supported in nearly every programming language.

# Find minimum and maximum element in an array

### C Code To Find Min and Max Value in Array

``````#include <stdio.h>

int main() {
int myArray[] = {5, 7, 2, 9, 1, 4, 6, 8, 3};
int arraySize = sizeof(myArray) / sizeof(myArray[0]);
int min = myArray[0];
int max = myArray[0];

for(int i = 1; i < arraySize; i++) {
if(myArray[i] < min) {
min = myArray[i];
}
if(myArray[i] > max) {
max = myArray[i];
}
}

printf("Min value: %dn", min);
printf("Max value: %dn", max);

return 0;
}
﻿``````

### Find Min and Max Value in Array In C++

``````#include <iostream>
#include <array>

int main() {
std::array<int, 9> myArray = {5, 7, 2, 9, 1, 4, 6, 8, 3};
int min = myArray[0];
int max = myArray[0];

for(int i = 1; i < myArray.size(); i++) {
if(myArray[i] < min) {
min = myArray[i];
}
if(myArray[i] > max) {
max = myArray[i];
}
}

std::cout << "Min value: " << min << std::endl;
std::cout << "Max value: " << max << std::endl;

return 0;
}
﻿``````

### Find Min and Max Value in Array In Python

``````my_array = [5, 7, 2, 9, 1, 4, 6, 8, 3]
min = my_array[0]
max = my_array[0]

for i in range(1, len(my_array)):
if my_array[i] < min:
min = my_array[i]
if my_array[i] > max:
max = my_array[i]

print("Min value:", min)
print("Max value:", max)
﻿``````

### Time and Space Complexity

The time complexity of the linear search approach is O(n). In the worst case, the algorithm will need to make n-1 comparisons to find the minimum or maximum element. The space complexity of this approach is O(1), since we only need to allocate space for two variables: min and max.

## Method 2: Sorting Algorithm

Find Min and Max Value in Array In C

``````#include <stdio.h>
#include <stdlib.h>

int main() {
int arr[] = {12, 45, 1, 98, 78, 36};
int n = sizeof(arr) / sizeof(arr[0]);
int i, j, temp;

// Sorting the array using bubble sort algorithm
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

printf("Min value in the array: %dn", arr[0]);
printf("Maximum value in the array: %dn", arr[n-1]);

return 0;
}
﻿``````

This code initializes an array of integers and finds its length using the `sizeof` operator. Then, it uses the bubble sort algorithm to sort the array in ascending order. Bubble sort compares adjacent elements of the array and swaps them if they are in the wrong order. It repeats this process until the array is fully sorted.

After sorting the array, the min value is the first element (`arr[0]`) and the maximum value is the last element (`arr[n-1]`), where `n` is the length of the array.

Find Min and Max Value in Array In C++

``````#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int arr[] = {12, 45, 1, 98, 78, 36};
int n = sizeof(arr) / sizeof(arr[0]);

// Sorting the array using sort function
sort(arr, arr+n);

cout << "Min value in the array: " << arr[0] << endl;
cout << "Maximum value in the array: " << arr[n-1] << endl;
return 0;
}
﻿``````

This code is similar to the C code, but it uses the `sort` function from the `<algorithm>` header to sort the array. The `sort` function takes two iterators as arguments: the beginning and end of the range to be sorted. In this case, we pass `arr` and `arr+n`, where `n` is the length of the array.

Find Min and Max Value in Array In Python

``````arr = [12, 45, 1, 98, 78, 36]

# Sorting the array using sorted function
arr = sorted(arr)

print("Minimum value in the array:", arr[0])
print("Maximum value in the array:", arr[-1])``````

This code initializes a list of integers and uses the `sorted` function to sort the list in ascending order. The `sorted` function returns a new sorted list, so we assign it back to the `arr` variable.

After sorting the list, the minimum value is the first element (`arr[0]`) and the maximum value is the last element (`arr[-1]`), which is the same as `arr[len(arr)-1]`.

### Method 3: Using Built In Functions

Find Min and Max Value in Array In C

``````#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main() {
int arr[] = { 12, 45, 1, 98, 78, 36 };
int n = sizeof(arr) / sizeof(arr[0]);

int min = INT_MAX, max = INT_MIN;

// Finding the minimum and maximum values in the array using built-in functions
for (int i = 0; i < n; i++) {
if (arr[i] < min)
min = arr[i];
if (arr[i] > max)
max = arr[i];
}

printf("Minimum value in the array: %dn", min);
printf("Maximum value in the array: %dn", max);

return 0;
}
﻿``````

#### C Code Explanation:

1. First, we initialize an array of integers called ‘arr’ with some values.
2. We also find the length of the array ‘n’ using the sizeof() function.
3. Then, we initialize two variables ‘min’ and ‘max’ to the maximum and minimum integer values respectively, using the constants INT_MAX and INT_MIN from the limits.h header file.
4. We then loop through the array using a for loop and check if each element is smaller than the current value of ‘min’ or greater than the current value of ‘max’.
5. If a smaller element is found, we update ‘min’ to that value.
6. Similarly, if a greater element is found, we update ‘max’ to that value.
7. Finally, we print out the values of ‘min’ and ‘max’ to the console.

Find Min and Max Value in Array In C++

``````#include <iostream>
#include <algorithm>
#include <limits>

using namespace std;

int main() {
int arr[] = { 12, 45, 1, 98, 78, 36 };
int n = sizeof(arr) / sizeof(arr[0]);

// Finding the minimum and maximum values in the array using built-in functions
int min = *min_element(arr, arr + n);
int max = *max_element(arr, arr + n);

cout << "Minimum value in the array: " << min << endl;
cout << "Maximum value in the array: " << max << endl;

return 0;
}
﻿``````

### C++ Code Explanation:

1. We start by initializing an array ‘arr’ with some values.
2. We find the length of the array ‘n’ using the sizeof() function.
3. We then make use of the min_element and max_element functions to find the minimum and maximum elements in the array respectively.
4. These functions return iterators pointing to the minimum and maximum elements in the array.
5. We then dereference these iterators using the * operator to get the actual values of the minimum and maximum elements.
6. Finally, we print out the values of the minimum and maximum elements to the console.

#### Find Min and Max Value in Array In Python

``````arr = [12, 45, 1, 98, 78, 36]

# Finding the minimum and maximum values in the array using built-in functions
min_val = min(arr)
max_val = max(arr)

print("Minimum value in the array: ", min_val)
print("Maximum value in the array: ", max_val)

﻿``````

#### Python Code Explanation:

1. We start by initializing an array ‘arr’ with some values.
2. We then make use of the built-in min and max functions in Python to find the minimum and maximum values in the array respectively.
3. These functions return the minimum and maximum values directly.
4. We store the minimum and maximum values in two variables called ‘min_val’ and ‘max_val’ respectively.
5. Finally, we print out the values of ‘min_val’ and ‘max_val’ to the console.

## Conclusion

In this article, we’ve explored several different methods for finding the min and max values in an array using various programming languages. While some methods may be more efficient than others, it’s important to understand the different approaches and their strengths and weaknesses.

## Number of Longest Increasing Subsequence LeetCode Solution

Number of Longest Increasing Subsequence LeetCode Solution Problem Description The Number of Longest Increasing Subsequence LeetCode Solution problem…

## ZigZag Conversion LeetCode Solution: How to Solve It

ZigZag Conversion: If you are a programmer or a coding enthusiast, you must have come across the term…

## Full Stack Developer Roadmap 2023

Full stack developer roadmap 2023: Full-stack development can prove to be an excellent career choice because professionals in…