CategoryC Program

C Program to find the difference between the largest and smallest elements

Learn how to find the difference between the largest and smallest elements in a C array using one traversal, with safe arithmetic and examples.

The difference between the largest and smallest elements measures the spread, or range, of the values in an array.

The formula is:

Difference = Largest element - Smallest element

For example:

Array: 12 -4 7 25 3
Largest element: 25
Smallest element: -4
Difference: 25 - (-4) = 29

We can find both extreme values during one traversal and then subtract the minimum from the maximum.

C Program to Find the Difference Between Largest and Smallest Elements

#include <stdio.h>

#define MAX_SIZE 100

int main(void) {
    int array[MAX_SIZE];
    int size;

    printf("Enter the number of elements: ");
    if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
        printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
        return 1;
    }

    printf("Enter %d elements:\n", size);
    for (int index = 0; index < size; index++) {
        if (scanf("%d", &array[index]) != 1) {
            printf("Invalid array element.\n");
            return 1;
        }
    }

    int smallest = array[0];
    int largest = array[0];

    for (int index = 1; index < size; index++) {
        if (array[index] < smallest) {
            smallest = array[index];
        }

        if (array[index] > largest) {
            largest = array[index];
        }
    }

    long long difference = (long long)largest - (long long)smallest;

    printf("Largest element = %d\n", largest);
    printf("Smallest element = %d\n", smallest);
    printf("Difference = %lld\n", difference);

    return 0;
}

Sample Output

Enter the number of elements: 5
Enter 5 elements:
12 -4 7 25 3
Largest element = 25
Smallest element = -4
Difference = 29

How the Program Works

  1. smallest and largest are initialized with the first array element.
  2. The loop starts at index 1 because the value at index 0 is already being tracked.
  3. If the current value is less than smallest, it becomes the new minimum.
  4. If the current value is greater than largest, it becomes the new maximum.
  5. After the traversal, the program subtracts smallest from largest.

Here is a dry run for 12, -4, 7, 25, 3:

Current valueSmallestLargestAction
121212Initialize both values
-4-412Update smallest
7-412No change
25-425Update largest
3-425No change

The final calculation is 25 - (-4), which equals 29.

Why Initialize with the First Element?

Initializing both values to 0 fails for some arrays:

  • For 4, 8, 12, an initial minimum of 0 would incorrectly remain the smallest value even though zero is not in the array.
  • For -9, -2, -6, an initial maximum of 0 would incorrectly remain the largest value.

Using array[0] guarantees that both initial values actually belong to the array.

Why Use long long for the Difference?

The largest and smallest elements have type int, but their mathematical difference may not fit in an int. For example, subtracting INT_MIN from INT_MAX exceeds the range of a typical 32-bit signed integer.

Both values are converted to long long before subtraction:

long long difference = (long long)largest - (long long)smallest;

Casting before the subtraction prevents signed int overflow. The result is printed with %lld because it has type long long.

What If All Elements Are Equal?

When every element has the same value, the largest and smallest elements are equal, so the difference is zero:

Array: 6 6 6 6
Largest element: 6
Smallest element: 6
Difference: 0

What About a One-Element Array?

For a one-element array, that value is both the largest and smallest element. Their difference is therefore 0.

Array: -7
Difference: -7 - (-7) = 0

Does It Work with Negative Numbers?

Yes. The comparison operators handle negative values normally. Subtracting a negative minimum increases the difference:

Array: -12 -3 -20 -8
Largest element: -3
Smallest element: -20
Difference: -3 - (-20) = 17

The result can never be negative because the largest element is always greater than or equal to the smallest element.

Why Not Sort the Array?

Sorting would place the smallest and largest values at opposite ends, but it generally requires O(n log n) time and changes the array order unless a copy is made.

A single traversal finds both values in O(n) time and leaves the array unchanged.

Time and Space Complexity

  • Time complexity: O(n), because each array element is examined once.
  • Extra space complexity: O(1), because the algorithm stores only the current minimum, maximum, difference, and a loop index.

Common Mistakes

  • Subtracting in the wrong order and producing a negative result.
  • Initializing the minimum or maximum to 0 rather than the first array element.
  • Performing the subtraction as int before assigning it to long long.
  • Sorting the array when a single traversal is sufficient.
  • Accessing array[0] without first ensuring that the array contains at least one element.
  • Calculating an absolute difference unnecessarily; largest - smallest is already nonnegative.

By tracking the minimum and maximum together, the program calculates their difference efficiently in one traversal without sorting or modifying the array.