Given an array **arr[]** of **N** integers, the task is to find the maximum difference between any two elements of the array.

**Examples:**

Input:arr[] = {2, 1, 5, 3}

Output:4

|5 – 1| = 4

Input:arr[] = {-10, 4, -9, -5}

Output:14

**Approach:** The maximum absolute difference in the array will always be the absolute difference between the minimum and the maximum element from the array.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the maximum ` `// absolute difference between ` `// any two elements of the array ` `int` `maxAbsDiff(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// To store the minimum and the maximum ` ` ` `// elements from the array ` ` ` `int` `minEle = arr[0]; ` ` ` `int` `maxEle = arr[0]; ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` `minEle = min(minEle, arr[i]); ` ` ` `maxEle = max(maxEle, arr[i]); ` ` ` `} ` ` ` ` ` `return` `(maxEle - minEle); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 1, 5, 3 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << maxAbsDiff(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the maximum ` ` ` `// absolute difference between ` ` ` `// any two elements of the array ` ` ` `static` `int` `maxAbsDiff(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` ` ` `// To store the minimum and the maximum ` ` ` `// elements from the array ` ` ` `int` `minEle = arr[` `0` `]; ` ` ` `int` `maxEle = arr[` `0` `]; ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) { ` ` ` `minEle = Math.min(minEle, arr[i]); ` ` ` `maxEle = Math.max(maxEle, arr[i]); ` ` ` `} ` ` ` ` ` `return` `(maxEle - minEle); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `[] arr = { ` `2` `, ` `1` `, ` `5` `, ` `3` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.print(maxAbsDiff(arr, n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the maximum ` `# absolute difference between ` `# any two elements of the array ` `def` `maxAbsDiff(arr, n): ` ` ` ` ` `# To store the minimum and the maximum ` ` ` `# elements from the array ` ` ` `minEle ` `=` `arr[` `0` `] ` ` ` `maxEle ` `=` `arr[` `0` `] ` ` ` `for` `i ` `in` `range` `(` `1` `, n): ` ` ` `minEle ` `=` `min` `(minEle, arr[i]) ` ` ` `maxEle ` `=` `max` `(maxEle, arr[i]) ` ` ` ` ` `return` `(maxEle ` `-` `minEle) ` ` ` `# Driver code ` `arr ` `=` `[` `2` `, ` `1` `, ` `5` `, ` `3` `] ` `n ` `=` `len` `(arr) ` `print` `(maxAbsDiff(arr, n)) ` ` ` `# This code is contributed ` `# by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the maximum ` ` ` `// absolute difference between ` ` ` `// any two elements of the array ` ` ` `static` `int` `maxAbsDiff(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// To store the minimum and the maximum ` ` ` `// elements from the array ` ` ` `int` `minEle = arr[0]; ` ` ` `int` `maxEle = arr[0]; ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `{ ` ` ` `minEle = Math.Min(minEle, arr[i]); ` ` ` `maxEle = Math.Max(maxEle, arr[i]); ` ` ` `} ` ` ` ` ` `return` `(maxEle - minEle); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] arr = { 2, 1, 5, 3 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.WriteLine(maxAbsDiff(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the maximum ` `// absolute difference between ` `// any two elements of the array ` `function` `maxAbsDiff(` `$arr` `, ` `$n` `) ` `{ ` ` ` ` ` `// To store the minimum and the maximum ` ` ` `// elements from the array ` ` ` `$minEle` `= ` `$arr` `[0]; ` ` ` `$maxEle` `= ` `$arr` `[0]; ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `$minEle` `= min(` `$minEle` `, ` `$arr` `[` `$i` `]); ` ` ` `$maxEle` `= max(` `$maxEle` `, ` `$arr` `[` `$i` `]); ` ` ` `} ` ` ` ` ` `return` `(` `$maxEle` `- ` `$minEle` `); ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `(2, 1, 5, 3); ` `$n` `= sizeof(` `$arr` `); ` `echo` `maxAbsDiff(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

**Output:**

4

Attention reader! Don’t stop learning now. Get hold of all the important **Java Foundation** and Collections concepts with the **Fundamentals of Java and Java Collections Course** at a student-friendly price and become industry ready.