Array Max & Min in C++
Find the largest and smallest values in a C++ array in one pass, starting from INT_MIN and INT_MAX. Traces 10 5 20 15 8 and shows why starting at 0 fails.
1#include <iostream>2#include <climits>3using namespace std;45int main() {6 int n;78 cout << "Enter number of elements: ";9 cin >> n;1011 int arr[n];1213 cout << "Enter " << n << " elements: ";14 for (int i = 0; i < n; i++) {15 cin >> arr[i];16 }1718 int max = INT_MIN, min = INT_MAX;1920 for (int i = 0; i < n; i++) {21 if (arr[i] > max) {22 max = arr[i];23 }24 if (arr[i] < min) {25 min = arr[i];26 }27 }2829 cout << "Maximum element: " << max << endl;30 cout << "Minimum element: " << min << endl;3132 return 0;33}
Sample Output
Maximum element: 20
20 beat every other value.
Minimum element: 5
5 was the smallest seen.
In this one
- How to read n values into an array with a loop
- Why max starts at INT_MIN and min at INT_MAX
- How one loop can update both max and min
- Why int arr[n] is a compiler extension, not standard C++
How it works
- 1
Read n (5) and declare arr[n].
- 2
Read the five values into arr[0] to arr[4].
- 3
Set max = INT_MIN and min = INT_MAX.
- 4
For each element, update max if larger and min if smaller.
- 5
Print max 20 and min 5.
Edge cases
n = 0
Enter 0 elements.
The loop never runs, so the program prints INT_MIN as the maximum and INT_MAX as the minimum. Check n > 0 before searching.
All negative values
Enter -4 -9 -2.
Because max started at INT_MIN, it correctly becomes -2. Starting at 0 would wrongly report 0.
What's going on
This program finds the maximum (largest) and minimum (smallest) elements in an array. Finding max and min is one of the most fundamental array operations, used extensively in data analysis, sorting algorithms, and problem-solving. The program:
Example:
Header Files Used
#include <iostream>
cout for output and cin for input#include <climits>
INT_MIN and INT_MAXUnderstanding INT_MIN and INT_MAX
INT_MIN:
INT_MAX:
Why use these for initialization?
Declaring Variables
int n;
int arr[n];
int max = INT_MIN, min = INT_MAX;
Variable `n`:
Variable `arr[n]`:
nVariable `max`:
Variable `min`:
Why these initial values?
Taking Input From User
cout << "Enter number of elements: ";
cin >> n;
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
Step 1: Get array size
Step 2: Get array elements
Example:
arr[0] = 10, arr[1] = 5, arr[2] = 20, arr[3] = 15, arr[4] = 8Finding Maximum Element
for (int i = 0; i < n; i++)
if (arr[i] > max)
max = arr[i];
How it works:
Initial: max = INT_MIN (very small number)
Iteration 1 (i = 0):
arr[0] (10) > max (INT_MIN) → truemax = 10Iteration 2 (i = 1):
arr[1] (5) > max (10) → falsemax remains 10Iteration 3 (i = 2):
arr[2] (20) > max (10) → truemax = 20Iteration 4 (i = 3):
arr[3] (15) > max (20) → falsemax remains 20Iteration 5 (i = 4):
arr[4] (8) > max (20) → falsemax remains 20Result: max = 20
Finding Minimum Element
for (int i = 0; i < n; i++)
if (arr[i] < min)
min = arr[i];
How it works:
Initial: min = INT_MAX (very large number)
Iteration 1 (i = 0):
arr[0] (10) < min (INT_MAX) → truemin = 10Iteration 2 (i = 1):
arr[1] (5) < min (10) → truemin = 5Iteration 3 (i = 2):
arr[2] (20) < min (5) → falsemin remains 5Iteration 4 (i = 3):
arr[3] (15) < min (5) → falsemin remains 5Iteration 5 (i = 4):
arr[4] (8) < min (5) → falsemin remains 5Result: min = 5
Why Initialize to INT_MIN and INT_MAX?
Alternative (wrong) approach:
1int max = 0, min = 0; // WRONG!
Problem:
Our approach:
max = INT_MIN → any number will be largermin = INT_MAX → any number will be smallerOptimized Version (Single Loop)
We can find both max and min in a single loop:
1for (int i = 0; i < n; i++) {2if (arr[i] > max) max = arr[i];3if (arr[i] < min) min = arr[i];4}
Advantages:
Our program uses this optimized approach!
Complete Example Walkthrough
Input: Array = [10, 5, 20, 15, 8]
Initialization:
max = INT_MIN (very small)min = INT_MAX (very large)Iteration 1 (arr[0] = 10):
10 > INT_MIN → true → max = 1010 < INT_MAX → true → min = 10Iteration 2 (arr[1] = 5):
5 > 10 → false → max stays 105 < 10 → true → min = 5Iteration 3 (arr[2] = 20):
20 > 10 → true → max = 2020 < 5 → false → min stays 5Iteration 4 (arr[3] = 15):
15 > 20 → false → max stays 2015 < 5 → false → min stays 5Iteration 5 (arr[4] = 8):
8 > 20 → false → max stays 208 < 5 → false → min stays 5Final Results:
max = 20min = 5Displaying the Results
cout << "Maximum element: " << max << endl;
cout << "Minimum element: " << min << endl;
Output:
Edge Cases
Case 1: All elements are same
Case 2: All negative numbers
Case 3: Single element
Case 4: Sorted array (ascending)
Case 5: Sorted array (descending)
Time Complexity
Time Complexity: O(n)
Space Complexity: O(n)
This is optimal! We must check every element at least once.
Alternative: Using First Element
Alternative initialization:
1int max = arr[0], min = arr[0];2for (int i = 1; i < n; i++) {3if (arr[i] > max) max = arr[i];4if (arr[i] < min) min = arr[i];5}
Advantages:
Disadvantages:
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why is max initialized to INT_MIN?
Pick an answer to check it.
Question 2
Valid indexes for an array of size 5 are:
Pick an answer to check it.
Questions people ask
Is int arr[n] valid C++?
Arrays with a size known only at run time are a GCC and Clang extension, not standard C++. vector<int> arr(n) is the portable choice.
Can I use the standard library?
Yes. *max_element(arr, arr + n) and *min_element(arr, arr + n) from <algorithm> do the same search.
Why one loop and not two?
Both comparisons can share one pass over the array, so you read each value once.