# Program to find if two numbers and their AM and HM are present in an array using STL

Given an array of Number and two values A and B, The task is to check the following Conditions:

1. If two numbers are present in the array or not.
2. If Yes, then their Arithmetic Mean and Harmonic Mean are also present in the same array or not.
3. If all conditions are satisfied, Print the Geometric Mean of the two numbers.

The respective means of the numbers can be formulated as follows: Examples:

Input: arr[] = {1.0, 2.0, 2.5, 3.0, 4.0, 4.5, 5.0, 6.0}, A = 3, B = 6.
Output : GM = 4.24
Explanation:
A = 3, B = 6 are present in the array.
AM = 4.5, HM = 4 are also present in the array.
So, GM = 4.24

Input: arr = {1.0, 2.0, 2.5, 3.0, 4.0, 4.5, 5.0, 6.0}, A = 4, B = 6.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• The idea is to use Hashing, using which we can simply store the array elements in a Hash container and use constant time O(1) operations to find and track the numbers and their means. Finally, the Geometric Mean is computed if all the conditions are satisfied by observing the simple relation AM * HM = GM2.
• A step-wise implementation of the above approach is as follows:

1. A Hash container is defined to store the array elements.
2. The arithmetic and harmonic means are calculated based on the formulae.
3. Simple conditional statements are used to find the elements in the Hash container in constant time.
4. Provided, all conditions are satisfied, the GM is calculated from above relation.

Below is the implementation of the above approach:

## C++

 `// C++ 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 ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the Arithmetic Mean  ` `// of 2 numbers ` `float` `ArithmeticMean(``float` `A, ``float` `B) ` `{ ` `    ``return` `(A + B) / 2; ` `} ` ` `  `// Function to find the Harmonic Mean ` `// of 2 numbers ` `float` `HarmonicMean(``float` `A, ``float` `B) ` `{ ` `    ``return` `(2 * A * B) / (A + B); ` `} ` ` `  `// Following function checks and computes the ` `// desired results based on the means ` `void` `CheckArithmeticHarmonic(``float` `arr[], ` `                             ``float` `A,  ` `                             ``float` `B, ``int` `N) ` `{ ` `     `  `    ``// Calculate means  ` `    ``float` `AM = ArithmeticMean(A, B); ` `    ``float` `HM = HarmonicMean(A, B); ` ` `  `    ``// Hash container (Set) to store elements ` `    ``unordered_set<``float``> Hash; ` ` `  `    ``// Insertion of array elements in the Set ` `    ``for` `(``int` `i = 0; i < N; i++) ` `    ``{ ` `        ``Hash.insert(arr[i]); ` `    ``} ` ` `  `    ``// Conditionals to check if numbers ` `    ``// are present in array by Hashing ` `    ``if` `(Hash.find(A) != Hash.end() ` `        ``&& Hash.find(B) != Hash.end()) { ` ` `  `        ``// Conditionals to check if the AM and HM ` `        ``// of the numbers are present in array ` `        ``if` `(Hash.find(AM) != Hash.end() ` `            ``&& Hash.find(HM) != Hash.end()) { ` `           `  `            ``// If all conditions are satisfied, ` `            ``// the Geometric Mean is calculated ` `            ``cout << ``"GM = "``; ` `            ``printf``(``"%0.2f"``, ``sqrt``(AM * HM)); ` `        ``} ` `        ``else` `        ``{ ` `            ``// If numbers are found but the  ` `            ``// respective AM and HM are not ` `            ``// found in the array ` `            ``cout << ``"AM and HM not found"``; ` `        ``} ` `    ``} ` `    ``else` `    ``{ ` `        ``// If none of the conditions are satisfied ` `        ``cout << ``"Numbers not found"``; ` `    ``} ` `} ` ` `  `int` `main() ` `{ ` `   `  `    ``float` `arr[] = {1.0, 2.0, 2.5, 3.0, 4.0, ` `                   ``4.5, 5.0, 6.0}; ` ` `  `    ``int` `N = ``sizeof``(arr)/``sizeof``(arr); ` `    ``float` `A = 3.0; ` `    ``float` `B = 6.0; ` `    ``CheckArithmeticHarmonic(arr, A, B, N); ` `    ``return` `0; ` `} `

## Python3

 `# Python3 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 ` `from` `math ``import` `sqrt ` ` `  `# Function to find the arithmetic mean  ` `# of 2 numbers ` `def` `ArithmeticMean(A, B): ` `    ``return` `(A ``+` `B) ``/` `2` ` `  `# Function to find the harmonic mean ` `# of 2 numbers ` `def` `HarmonicMean(A, B): ` `    ``return` `(``2` `*` `A ``*` `B) ``/` `(A ``+` `B) ` ` `  `# Following function checks and computes the ` `# desired results based on the means ` `def` `CheckArithmeticHarmonic(arr, A, B, N): ` `     `  `    ``# Calculate means  ` `    ``AM ``=` `ArithmeticMean(A, B) ` `    ``HM ``=` `HarmonicMean(A, B) ` ` `  `    ``# Hash container (set) to store elements ` `    ``Hash` `=` `set``() ` ` `  `    ``# Insertion of array elements in the set ` `    ``for` `i ``in` `range``(N): ` `        ``Hash``.add(arr[i]) ` ` `  `    ``# Conditionals to check if numbers ` `    ``# are present in array by Hashing ` `    ``if` `(A ``in` `Hash` `and` `B ``in` `Hash``): ` `         `  `        ``# Conditionals to check if the AM and HM ` `        ``# of the numbers are present in array ` `        ``if` `(AM ``in` `Hash` `and` `HM ``in` `Hash``): ` `             `  `            ``# If all conditions are satisfied, ` `            ``# the Geometric Mean is calculated ` `            ``print``(``"GM ="``, ``round``(sqrt(AM ``*` `HM), ``2``)) ` `        ``else``: ` `             `  `            ``# If numbers are found but the  ` `            ``# respective AM and HM are not ` `            ``# found in the array ` `            ``print``(``"AM and HM not found"``) ` `    ``else``: ` `         `  `        ``# If none of the conditions are satisfied ` `        ``print``(``"Numbers not found"``) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` ` `  `    ``arr ``=` `[ ``1.0``, ``2.0``, ``2.5``, ``3.0``, ` `            ``4.0``, ``4.5``, ``5.0``, ``6.0` `] ` `    ``N ``=` `len``(arr) ` `    ``A ``=` `3.0` `    ``B ``=` `6.0` `     `  `    ``CheckArithmeticHarmonic(arr, A, B, N) ` ` `  `# This code is contributed by Samarth `

Output:

```GM = 4.24
```

Complexity Analysis:
The overall time complexity of the above program is based on the initial iteration of the array elements at user-defined input. The find operations associated with the Set are all O(1) constant-time operations. Therefore, the complexity of the program is O(N) where N is the size of the array. My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : ipg2016107