Maximum possible remainder when an element is divided by other element in the array | Function to return the maximum mod value for any pair from the array ; Find the second maximum element from the array ; Driver code
function maxMod ( arr , n ) { let maxVal = arr . sort ( ( a , b ) => b - a ) [ 0 ] let secondMax = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < maxVal && arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } } return secondMax ; } let arr = [ 2 , 4 , 1 , 5 , 3 , 6 ] ; let n = arr . length ; document . write ( maxMod ( arr , n ) ) ;