Range and Coefficient of range of Array
Last Updated :
09 Sep, 2022
Given an array arr of integer elements, the task is to find the range and coefficient of range of the given array where:
Range: Difference between the maximum value and the minimum value in the distribution.
Coefficient of Range: (Max – Min) / (Max + Min).
Examples:
Input: arr[] = {15, 16, 10, 9, 6, 7, 17}
Output: Range : 11
Coefficient of Range : 0.478261
Max = 17, Min = 6
Range = Max – Min = 17 – 6 = 11
Coefficient of Range = (Max – Min) / (Max + Min) = 11 / 23 = 0.478261
Input: arr[] = {5, 10, 15}
Output: Range : 10
Coefficient of Range : 0.5
Approach: Find the maximum and minimum element from the given array and calculate the range and the coefficient of range as follows:
- Range = Max – Min
- Coefficient of Range = (Max – Min) / (Max + Min)
Below is the implementation of the above approach:
C++
#include <iostream>
#include <numeric>
using namespace std;
float getMin( float arr[], int n)
{
float res = arr[0];
for ( int i = 1; i < n; i++)
res = min(res, arr[i]);
return res;
}
float getMax( float arr[], int n)
{
float res = arr[0];
for ( int i = 1; i < n; i++)
res = max(res, arr[i]);
return res;
}
void findRangeAndCoefficient( float arr[], int n)
{
float max = getMax(arr, n);
float min = getMin(arr, n);
float range = max - min;
float coeffOfRange = range / (max + min);
cout << "Range : " << range << endl;
cout << "Coefficient of Range : " << coeffOfRange;
}
int main()
{
float arr[] = { 5, 10, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
findRangeAndCoefficient(arr, n);
return 0;
}
|
C
#include <stdio.h>
int min( int a, int b)
{
int min = a;
if (min > b)
min = b;
return min;
}
int max( int a, int b)
{
int max = a;
if (max < b)
max = b;
return max;
}
float getMin( float arr[], int n)
{
float res = arr[0];
for ( int i = 1; i < n; i++)
res = min(res, arr[i]);
return res;
}
float getMax( float arr[], int n)
{
float res = arr[0];
for ( int i = 1; i < n; i++)
res = max(res, arr[i]);
return res;
}
void findRangeAndCoefficient( float arr[], int n)
{
float max = getMax(arr, n);
float min = getMin(arr, n);
float range = max - min;
float coeffOfRange = range / (max + min);
printf ( "Range : %f\n" ,range);
printf ( "Coefficient of Range : %f\n" ,coeffOfRange);
}
int main()
{
float arr[] = { 5, 10, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
findRangeAndCoefficient(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static float getMin( float arr[], int n)
{
float res = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
res = Math.min(res, arr[i]);
return res;
}
static float getMax( float arr[], int n)
{
float res = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
res = Math.max(res, arr[i]);
return res;
}
static void findRangeAndCoefficient( float arr[], int n)
{
float max = getMax(arr, n);
float min = getMin(arr, n);
float range = max - min;
float coeffOfRange = range / (max + min);
System.out.println( "Range : " + range );
System.out.println( "Coefficient of Range : " + coeffOfRange);
}
public static void main (String[] args) {
float arr[] = { 5 , 10 , 15 };
int n = arr.length;
findRangeAndCoefficient(arr, n);
}
}
|
Python3
def getMin(arr, n):
res = arr[ 0 ]
for i in range ( 1 , n, 1 ):
res = min (res, arr[i])
return res
def getMax(arr, n):
res = arr[ 0 ]
for i in range ( 1 , n, 1 ):
res = max (res, arr[i])
return res
def findRangeAndCoefficient(arr, n):
max = getMax(arr, n)
min = getMin(arr, n)
range = max - min
coeffOfRange = range / ( max + min )
print ( "Range :" , range )
print ( "Coefficient of Range :" , coeffOfRange)
if __name__ = = '__main__' :
arr = [ 5 , 10 , 15 ]
n = len (arr)
findRangeAndCoefficient(arr, n)
|
C#
using System;
public class GFG{
static float getMin( float []arr, int n)
{
float res = arr[0];
for ( int i = 1; i < n; i++)
res = Math.Min(res, arr[i]);
return res;
}
static float getMax( float []arr, int n)
{
float res = arr[0];
for ( int i = 1; i < n; i++)
res = Math.Max(res, arr[i]);
return res;
}
static void findRangeAndCoefficient( float []arr, int n)
{
float max = getMax(arr, n);
float min = getMin(arr, n);
float range = max - min;
float coeffOfRange = range / (max + min);
Console.WriteLine ( "Range : " + range );
Console.WriteLine ( "Coefficient of Range : " + coeffOfRange);
}
static public void Main (){
float []arr = { 5, 10, 15 };
int n = arr.Length;
findRangeAndCoefficient(arr, n);
}
}
|
PHP
<?php
function getMin( $arr , $n )
{
$res = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
$res = min( $res , $arr [ $i ]);
return $res ;
}
function getMax( $arr , $n )
{
$res = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
$res = max( $res , $arr [ $i ]);
return $res ;
}
function findRangeAndCoefficient( $arr , $n )
{
$max = getMax( $arr , $n );
$min = getMin( $arr , $n );
$range = $max - $min ;
$coeffOfRange = $range / ( $max + $min );
echo "Range : " , $range , "\n" ;
echo "Coefficient of Range : " ,
$coeffOfRange ;
}
$arr = array ( 5, 10, 15 );
$n = sizeof( $arr );
findRangeAndCoefficient( $arr , $n );
?>
|
Javascript
<script>
function getMin(arr, n)
{
let res = arr[0];
for (let i = 1; i < n; i++)
res = Math.min(res, arr[i]);
return res;
}
function getMax(arr, n)
{
let res = arr[0];
for (let i = 1; i < n; i++)
res = Math.max(res, arr[i]);
return res;
}
function findRangeAndCoefficient(arr, n)
{
let max = getMax(arr, n);
let min = getMin(arr, n);
let range = max - min;
let coeffOfRange = range / (max + min);
document.write( "Range : " + range + "</br>" );
document.write( "Coefficient of Range : " +
coeffOfRange + "</br>" );
}
let arr = [ 5, 10, 15 ];
let n = arr.length;
findRangeAndCoefficient(arr, n);
</script>
|
Output
Range : 10
Coefficient of Range : 0.5
Complexity Analysis:
- Time complexity : O(n)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...