Dada una array arr de elementos enteros, la tarea es encontrar el rango y el coeficiente de rango de la array dada donde:
Rango: Diferencia entre el valor máximo y el valor mínimo en la distribución.
Coeficiente de Rango: (Max – Min) / (Max + Min).
Ejemplos:
Entrada: arr[] = {15, 16, 10, 9, 6, 7, 17}
Salida: Rango: 11
Coeficiente de rango: 0,478261
Max = 17, Min = 6
Rango = Max – Min = 17 – 6 = 11
Coeficiente de rango = (Máx. – Mín.) / (Máx. + Mín.) = 11 / 23 = 0,478261
Entrada: arr[] = {5, 10, 15}
Salida: Rango: 10
Coeficiente de rango: 0,5
Enfoque: encuentre el elemento máximo y mínimo de la array dada y calcule el rango y el coeficiente de rango de la siguiente manera:
- Rango = Máx – Mín
- Coeficiente de Rango = (Max – Min) / (Max + Min)
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to find // Range and coefficient of range #include <iostream> #include <numeric> using namespace std; // Function to return the minimum element from the array float getMin(float arr[], int n) { float res = arr[0]; for (int i = 1; i < n; i++) res = min(res, arr[i]); return res; } // Function to return the maximum element from the array float getMax(float arr[], int n) { float res = arr[0]; for (int i = 1; i < n; i++) res = max(res, arr[i]); return res; } // Function to print the Range and // Coefficient of Range in the given array 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; } // Driver code int main() { float arr[] = { 5, 10, 15 }; int n = sizeof(arr) / sizeof(arr[0]); findRangeAndCoefficient(arr, n); return 0; }
C
// C implementation to find // Range and coefficient of range #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; } // Function to return the minimum element from the array float getMin(float arr[], int n) { float res = arr[0]; for (int i = 1; i < n; i++) res = min(res, arr[i]); return res; } // Function to return the maximum element from the array float getMax(float arr[], int n) { float res = arr[0]; for (int i = 1; i < n; i++) res = max(res, arr[i]); return res; } // Function to print the Range and // Coefficient of Range in the given array 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); } // Driver code int main() { float arr[] = { 5, 10, 15 }; int n = sizeof(arr) / sizeof(arr[0]); findRangeAndCoefficient(arr, n); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java implementation to find // Range and coefficient of range import java.io.*; class GFG { // Function to return the minimum element from the array 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; } // Function to return the maximum element from the array 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; } // Function to print the Range and // Coefficient of Range in the given array 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); } // Driver code public static void main (String[] args) { float arr[] = { 5, 10, 15 }; int n = arr.length; findRangeAndCoefficient(arr, n); } }
Python3
# Python 3 implementation to find # Range and coefficient of range # Function to return the minimum # element from the array def getMin(arr, n): res = arr[0] for i in range(1, n, 1): res = min(res, arr[i]) return res # Function to return the maximum # element from the array def getMax(arr, n): res = arr[0] for i in range(1, n, 1): res = max(res, arr[i]) return res # Function to print the Range and # Coefficient of Range in the given array 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) # Driver code if __name__ == '__main__': arr = [5, 10, 15] n = len(arr) findRangeAndCoefficient(arr, n) # This code is contributed by # Surendra_Gangwar
C#
// C# implementation to find // Range and coefficient of range using System; public class GFG{ // Function to return the minimum element from the array 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; } // Function to return the maximum element from the array 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; } // Function to print the Range and // Coefficient of Range in the given array 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); } // Driver code static public void Main (){ float []arr = { 5, 10, 15 }; int n = arr.Length; findRangeAndCoefficient(arr, n); } //This code is contributed by akt_mit. }
PHP
<?php // PHP implementation to find // Range and coefficient of range // Function to return the minimum // element from the array function getMin($arr, $n) { $res = $arr[0]; for ($i = 1; $i < $n; $i++) $res = min($res, $arr[$i]); return $res; } // Function to return the maximum // element from the array function getMax($arr, $n) { $res = $arr[0]; for ($i = 1; $i < $n; $i++) $res = max($res, $arr[$i]); return $res; } // Function to print the Range and // Coefficient of Range in the given array 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; } // Driver code $arr = array( 5, 10, 15 ); $n = sizeof($arr); findRangeAndCoefficient($arr, $n); // This code is contributed by jit_t ?>
Javascript
<script> // Javascript implementation to find // Range and coefficient of range // Function to return the minimum // element from the array function getMin(arr, n) { let res = arr[0]; for (let i = 1; i < n; i++) res = Math.min(res, arr[i]); return res; } // Function to return the maximum // element from the array function getMax(arr, n) { let res = arr[0]; for (let i = 1; i < n; i++) res = Math.max(res, arr[i]); return res; } // Function to print the Range and // Coefficient of Range in the given array 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>
Range : 10 Coefficient of Range : 0.5
Complejidad temporal : O(n) Espacio auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por VishalBachchas y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA