Maximum difference between two elements in an Array | C ++ implementation of the approach ; Function to return the maximum absolute difference between any two elements of the array ; To store the minimum and the maximum elements from the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxAbsDiff ( int arr [ ] , int n ) { 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 ) ; } int main ( ) { int arr [ ] = { 2 , 1 , 5 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxAbsDiff ( arr , n ) ; return 0 ; }