# Replace the maximum element in the array by coefficient of range

Given an array arr[] of integer elements, the task is to replace the maximum element in the array by the coefficient of range of the same array.
Coefficient of Range: (Max – Min) / (Max + Min)

Examples:

Input: arr[] = {15, 16, 10, 9, 6, 7, 17}
Output: 15 16 10 9 6 7 0.478261
Max = 17, Min = 6
Coefficient of Range = (Max – Min) / (Max + Min) = 11 / 23 = 0.478261

Input: arr[] = {5, 10, 15}
Output: 5 10 0.5

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Find the maximum and minimum element from the given array and calculate the coefficient of range, coeff = (Max – Min) / (Max + Min) then replace maximum element by the coeff calculated. After the array has been updated, print the contents of the updated array.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to replace maximum element ` `// by coefficient of range ` `#include ` `using` `namespace` `std; ` ` `  `// Utility function to print the contents of the array ` `void` `printArr(``float` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << arr[i] << ``" "``; ` `} ` ` `  `// Function to replace the maximum element from the array ` `// with the coefficient of range of the array ` `void` `replaceMax(``float` `arr[], ``int` `n) ` `{ ` ` `  `    ``// Maximum element from the array ` `    ``float` `max = *std::max_element(arr, arr + n); ` ` `  `    ``// Minimum element from the array ` `    ``float` `min = *std::min_element(arr, arr + n); ` ` `  `    ``// Calculate the coefficient of range for the array ` `    ``float` `range = max - min; ` `    ``float` `coeffOfRange = range / (max + min); ` ` `  `    ``// Assuming all the array elements are distinc ` `    ``// Replace the maximum element with ` `    ``// the coeffient of range of the array ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(arr[i] == max) { ` `            ``arr[i] = coeffOfRange; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// Print the updated array ` `    ``printArr(arr, n); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``float` `arr[] = { 15, 16, 10, 9, 6, 7, 17 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``replaceMax(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation to replace maximum element ` `// by coefficient of range ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `// Utility function to print the  ` `// contents of the array ` `static` `void` `printArr(``float` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``System.out.print(arr[i] + ``" "``); ` `} ` ` `  `// Function to replace the maximum  ` `// element from the array with the  ` `// coefficient of range of the array ` `static` `void` `replaceMax(``float` `arr[], ``int` `n) ` `{ ` `    ``// Maximum element from the array ` `    ``float` `max = arr[``0``]; ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{  ` `        ``if``(arr[i] > max) ` `        ``max = arr[i]; ` `    ``}  ` `    ``// Minimum element from the arra ` `    ``float` `min = arr[``0``]; ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{  ` `        ``if``(arr[i] < min) ` `        ``min = arr[i]; ` `    ``}  ` ` `  `    ``// Calculate the coefficient of  ` `    ``// range for the array ` `    ``float` `range = max - min; ` `    ``float` `coeffOfRange = range / (max + min); ` ` `  `    ``// Assuming all the array elements are distinc ` `    ``// Replace the maximum element with ` `    ``// the coeffient of range of the array ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` `        ``if` `(arr[i] == max)  ` `        ``{ ` `            ``arr[i] = coeffOfRange; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// Print the updated array ` `    ``printArr(arr, n); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``float` `arr[] = { ``15``, ``16``, ``10``, ``9``, ``6``, ``7``, ``17` `}; ` `    ``int` `n = arr.length; ` `    ``replaceMax(arr, n); ` ` `  `} ` `} ` ` `  `// This code is contributed by ` `// Sahil_Shelangia `

## Python3

 `# Python3 implementation to replace  ` `# maximum element by coefficient of range  ` ` `  `# Utility function to print the ` `# contents of the array  ` `def` `printArr(arr, n) : ` `     `  `    ``for` `i ``in` `range``(n) : ` `        ``print``(arr[i], end ``=` `" "``) ` ` `  `# Function to replace the maximum element  ` `# from the array with the coefficient of  ` `# range of the array  ` `def` `replaceMax(arr, n) : ` ` `  `    ``# Maximum element from the array  ` `    ``max_element ``=` `max``(arr) ` ` `  `    ``# Minimum element from the array  ` `    ``min_element ``=` `min``(arr) ` ` `  `    ``# Calculate the coefficient of  ` `    ``# range for the array  ` `    ``ranges ``=` `max_element ``-` `min_element ` `    ``coeffOfRange ``=` `ranges ``/` `(max_element ``+` `min_element)  ` ` `  `    ``# Assuming all the array elements are  ` `    ``# distinct. Replace the maximum element  ` `    ``# with the coeffient of range of the array  ` `    ``for` `i ``in` `range``(n) : ` `        ``if` `(arr[i] ``=``=` `max_element) : ` `            ``arr[i] ``=` `coeffOfRange ` `            ``break` ` `  `    ``# Print the updated array  ` `    ``printArr(arr, n) ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``arr ``=` `[ ``15``, ``16``, ``10``, ``9``, ``6``, ``7``, ``17` `] ` `    ``n ``=` `len``(arr)  ` `     `  `    ``replaceMax(arr, n) ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# implementation to replace maximum element ` `// by coefficient of range ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Utility function to print the  ` `// contents of the array ` `static` `void` `printArr(``float` `[]arr, ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``Console.Write(arr[i] + ``" "``); ` `} ` ` `  `// Function to replace the maximum  ` `// element from the array with the  ` `// coefficient of range of the array ` `static` `void` `replaceMax(``float` `[]arr, ``int` `n) ` `{ ` `    ``// Maximum element from the array ` `    ``float` `max = arr; ` `    ``for``(``int` `i = 0; i < n; i++) ` `    ``{  ` `        ``if``(arr[i] > max) ` `        ``max = arr[i]; ` `    ``}  ` `     `  `    ``// Minimum element from the arra ` `    ``float` `min = arr; ` `    ``for``(``int` `i = 0; i < n; i++) ` `    ``{  ` `        ``if``(arr[i] < min) ` `        ``min = arr[i]; ` `    ``}  ` ` `  `    ``// Calculate the coefficient of  ` `    ``// range for the array ` `    ``float` `range = max - min; ` `    ``float` `coeffOfRange = range / (max + min); ` ` `  `    ``// Assuming all the array elements are distinc ` `    ``// Replace the maximum element with ` `    ``// the coeffient of range of the array ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` `        ``if` `(arr[i] == max)  ` `        ``{ ` `            ``arr[i] = coeffOfRange; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// Print the updated array ` `    ``printArr(arr, n); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``float` `[]arr = { 15, 16, 10, 9, 6, 7, 17 }; ` `    ``int` `n = arr.Length; ` `    ``replaceMax(arr, n); ` `} ` `} ` ` `  `// This code is contributed by ` `// shs.. `

## PHP

 ` `

Output:

```15 16 10 9 6 7 0.478261
```

Time Complexity : O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.