Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

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.

BeginnerArray ProgramsExample 1 of 4
array-max-min.cpp
1#include <iostream>
2#include <climits>
3using namespace std;
4
5int main() {
6 int n;
7
8 cout << "Enter number of elements: ";
9 cin >> n;
10
11 int arr[n];
12
13 cout << "Enter " << n << " elements: ";
14 for (int i = 0; i < n; i++) {
15 cin >> arr[i];
16 }
17
18 int max = INT_MIN, min = INT_MAX;
19
20 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 }
28
29 cout << "Maximum element: " << max << endl;
30 cout << "Minimum element: " << min << endl;
31
32 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. 1

    Read n (5) and declare arr[n].

  2. 2

    Read the five values into arr[0] to arr[4].

  3. 3

    Set max = INT_MIN and min = INT_MAX.

  4. 4

    For each element, update max if larger and min if smaller.

  5. 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:

Takes an array of numbers as input
Finds the largest number (maximum) in the array
Finds the smallest number (minimum) in the array
Displays both results

Example:

Array: [10, 5, 20, 15, 8]
Maximum: 20 (largest value)
Minimum: 5 (smallest value)

Header Files Used

#include <iostream>

Provides cout for output and cin for input

#include <climits>

Provides constants for integer limits
Contains INT_MIN and INT_MAX
Essential for proper initialization

Understanding INT_MIN and INT_MAX

INT_MIN:

Smallest possible integer value
Typically: -2,147,483,648 (on 32-bit systems)
Any integer is greater than or equal to INT_MIN

INT_MAX:

Largest possible integer value
Typically: 2,147,483,647 (on 32-bit systems)
Any integer is less than or equal to INT_MAX

Why use these for initialization?

We want max to be smaller than any possible array element
We want min to be larger than any possible array element
This ensures first comparison always updates the value

Declaring Variables

int n;

int arr[n];

int max = INT_MIN, min = INT_MAX;

Variable `n`:

Stores the number of elements in the array
Used to create array and control loops

Variable `arr[n]`:

Array to store the input numbers
Size is determined by n

Variable `max`:

Stores the maximum element found so far
Initialized to INT_MIN - ensures any number will be larger

Variable `min`:

Stores the minimum element found so far
Initialized to INT_MAX - ensures any number will be smaller

Why these initial values?

First array element will always be:
Greater than INT_MIN → updates max
Less than INT_MAX → updates min

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

User enters how many numbers they want to input
Example: 5

Step 2: Get array elements

Loop reads n numbers
Stores them in array: arr[0], arr[1], ..., arr[n-1]

Example:

User enters: 10 5 20 15 8
arr[0] = 10, arr[1] = 5, arr[2] = 20, arr[3] = 15, arr[4] = 8

Finding 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):

Check: arr[0] (10) > max (INT_MIN) → true
Update: max = 10

Iteration 2 (i = 1):

Check: arr[1] (5) > max (10) → false
max remains 10

Iteration 3 (i = 2):

Check: arr[2] (20) > max (10) → true
Update: max = 20

Iteration 4 (i = 3):

Check: arr[3] (15) > max (20) → false
max remains 20

Iteration 5 (i = 4):

Check: arr[4] (8) > max (20) → false
max remains 20

Result: 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):

Check: arr[0] (10) < min (INT_MAX) → true
Update: min = 10

Iteration 2 (i = 1):

Check: arr[1] (5) < min (10) → true
Update: min = 5

Iteration 3 (i = 2):

Check: arr[2] (20) < min (5) → false
min remains 5

Iteration 4 (i = 3):

Check: arr[3] (15) < min (5) → false
min remains 5

Iteration 5 (i = 4):

Check: arr[4] (8) < min (5) → false
min remains 5

Result: min = 5

Why Initialize to INT_MIN and INT_MAX?

Alternative (wrong) approach:

1int max = 0, min = 0; // WRONG!

Problem:

If all numbers are negative: max would stay 0 (wrong!)
If all numbers are positive: min would stay 0 (wrong!)

Our approach:

max = INT_MIN → any number will be larger
min = INT_MAX → any number will be smaller
Works correctly for any input (positive, negative, or mixed)

Optimized 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:

Only one loop instead of two
More efficient (fewer iterations)
Same time complexity, but faster in practice

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 = 10
10 < INT_MAX → true → min = 10

Iteration 2 (arr[1] = 5):

5 > 10 → false → max stays 10
5 < 10 → true → min = 5

Iteration 3 (arr[2] = 20):

20 > 10 → true → max = 20
20 < 5 → false → min stays 5

Iteration 4 (arr[3] = 15):

15 > 20 → false → max stays 20
15 < 5 → false → min stays 5

Iteration 5 (arr[4] = 8):

8 > 20 → false → max stays 20
8 < 5 → false → min stays 5

Final Results:

max = 20
min = 5

Displaying the Results

cout << "Maximum element: " << max << endl;

cout << "Minimum element: " << min << endl;

Output:

Maximum element: 20
Minimum element: 5

Edge Cases

Case 1: All elements are same

Array: [5, 5, 5, 5]
max = 5, min = 5

Case 2: All negative numbers

Array: [-10, -5, -20]
max = -5, min = -20
INT_MIN initialization ensures this works

Case 3: Single element

Array: [42]
max = 42, min = 42

Case 4: Sorted array (ascending)

Array: [1, 2, 3, 4, 5]
max = 5 (last element), min = 1 (first element)

Case 5: Sorted array (descending)

Array: [5, 4, 3, 2, 1]
max = 5 (first element), min = 1 (last element)

Time Complexity

Time Complexity: O(n)

We iterate through the array once
n comparisons for max, n comparisons for min
Total: 2n comparisons = O(n)

Space Complexity: O(n)

Array storage: O(n)
Variables: O(1)
Total: 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:

No need for INT_MIN/INT_MAX
Start loop from index 1 (slightly faster)

Disadvantages:

Requires array to have at least one element
Need to handle empty array separately

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.