Program to find if two numbers and their AM and HM are present in an array using STL | Function to find the Arithmetic Mean of 2 numbers ; Function to find the Harmonic Mean of 2 numbers ; Following function checks and computes the desired results based on the means ; Calculate means ; Hash container ( HashMap ) to store elements ; Insertion of array elements in the Set ; Conditionals to check if numbers are present in array by Hashing ; Conditionals to check if the AM and HM of the numbers are present in array ; If all conditions are satisfied , the Geometric Mean is calculated ; If numbers are found but the respective AM and HM are not found in the array ; If none of the conditions are satisfied ; Driver code
function ArithmeticMean ( A , B ) { return ( ( A + B ) / 2 ) ; } function HarmonicMean ( A , B ) { return ( 2 * A * B ) / ( A + B ) ; } function CheckArithmeticHarmonic ( arr , A , B , N ) { let AM = ArithmeticMean ( A , B ) ; let HM = HarmonicMean ( A , B ) ; let Hash = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { Hash . set ( arr [ i ] , 1 ) ; } if ( Hash . get ( A ) != 0 && Hash . get ( B ) != 0 ) { if ( Hash . get ( AM ) != 0 && Hash . get ( HM ) != 0 ) { document . write ( " " ) ; document . write ( Math . sqrt ( AM * HM ) . toFixed ( 2 ) ) ; } else { document . write ( " " ) ; } } else { document . write ( " " ) ; } } let arr = [ 1.0 , 2.0 , 2.5 , 3.0 , 4.0 , 4.5 , 5.0 , 6.0 ] ; let N = ( arr . length ) ; let A = 3.0 ; let B = 6.0 ; CheckArithmeticHarmonic ( arr , A , B , N ) ;