How to Find the Range of Numbers in an Array in C? (original) (raw)

Last Updated : 20 Jun, 2024

The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C.

**Example

**Input:
int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }

**Output:
The range of the array is : 83

Range of Numbers Within an Array in C

To find the range of numbers in an array in C, first we have to iterate through an array for find the minimum and the maximum element and then calculate the range which is the difference of maximum and minimum element of an array.

Approach

C Program to Find the Range of Numbers in an Array

The following program illustrates how we can find the range of numbers in an array in C.

C `

// C Program to Find the Range of Numbers in an Array

#include <stdio.h>

int main() {

// Initialize an array
int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 };

// Finding size of an array
int n = sizeof(arr) / sizeof(arr[0]);

// Intialize the first element of the array as both
// maximum and minimum value
int min = arr[0];
int max = arr[0];

// Find the maximum and minimum value in the array
for (int i = 1; i < n; i++) {
    if (max < arr[i])
        max = arr[i];

    if (min > arr[i])
        min = arr[i];
}
// Calculating the range of an array
int range = max - min;

// Printing the range of an array
printf("The range of the array is %d:", range);

return 0;

}

`

Output

The range of the array is 83:

**Time Complexity: O(N), here N is the size of the array.
**Auxiliary Space: O(1)