![]() |
VOOZH | about |
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
To find the range of numbers in an 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.
- Find the size of the array and store it in a variable n.
- Declare two variables min and max to find the maximum and minimum value from the array.
- Initialize both the min and max to the first element of the array.
- Keep iterating through the rest of the array and update the min and max variables.
- Find the range of the array by subtracting the value of min from the max.
- Return the value of range as a result.
The following program illustrates how we can find the range of numbers in an array in C.
The range of the array is 83:
Time Complexity: O(N), here N is the size of the array.
Auxiliary Space: O(1)