Program to find if two numbers and their AM and HM are present in an array using STL | Java program to check if two numbers are present in an array then their AM and HM are also present . Finally , find the GM of the numbers ; 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
import java . util . * ; class GFG { static Double ArithmeticMean ( Double A , Double B ) { return ( A + B ) / 2 ; } static Double HarmonicMean ( Double A , Double B ) { return ( 2 * A * B ) / ( A + B ) ; } static void CheckArithmeticHarmonic ( Double arr [ ] , Double A , Double B , int N ) { Double AM = ArithmeticMean ( A , B ) ; Double HM = HarmonicMean ( A , B ) ; HashMap < Double , Integer > Hash = new HashMap < Double , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Hash . put ( arr [ i ] , 1 ) ; } if ( Hash . get ( A ) != 0 && Hash . get ( B ) != 0 ) { if ( Hash . get ( AM ) != 0 && Hash . get ( HM ) != 0 ) { System . out . print ( " GM ▁ = ▁ " ) ; System . out . format ( " % .2f " , Math . sqrt ( AM * HM ) ) ; } else { System . out . print ( " AM ▁ and ▁ HM ▁ not ▁ found " ) ; } } else { System . out . print ( " numbers ▁ not ▁ found " ) ; } } public static void main ( String args [ ] ) { Double arr [ ] = { 1.0 , 2.0 , 2.5 , 3.0 , 4.0 , 4.5 , 5.0 , 6.0 } ; int N = ( arr . length ) ; Double A = 3.0 ; Double B = 6.0 ; CheckArithmeticHarmonic ( arr , A , B , N ) ; } }