
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find a subset with greatest geometric mean in C++
Here we have an array A with some elements. Our task is to find the subset where the geometric mean is maximum. Suppose A = [1, 5, 7, 2, 0], then the subset with greatest geometric mean will be [5, 7].
To solve this, we will follow one trick, we will not find the mean, as we know that the largest two elements will form the greatest geometric mean, so the largest two elements will be returned as subset.
Example
#include <iostream> using namespace std; void largestGeoMeanSubset(int arr[], int n) { if (n < 2) { cout << "Very few number of elements"; return; } int max = INT_MIN, second_max = INT_MIN; for (int i = 0; i < n ; i ++) { if (arr[i] > max) { second_max = max; max = arr[i]; }else if (arr[i] > second_max) second_max = arr[i]; } cout << second_max << ", "<< max; } int main() { int arr[] = {1, 5, 7, 2, 0}; int n = sizeof(arr)/sizeof(arr[0]); largestGeoMeanSubset(arr, n); }
Output
5, 7
- Related Articles
- Find Harmonic mean using Arithmetic mean and Geometric mean using C++.
- Swift Program to Find Geometric Mean of the Numbers
- How to Calculate the Geometric Mean of Return
- Find the missing number in Geometric Progression in C++
- Find N Geometric Means between A and B using C++.
- Find all triplets in a sorted array that forms Geometric Progression in C++
- Subset with maximum sum in JavaScript
- Subarray with the greatest product in JavaScript
- Return numbers spaced evenly on a geometric progression but with negative inputs in Numpy
- Return numbers spaced evenly on a geometric progression but with complex inputs in Numpy
- Geometric Distribution in Data Structures
- Find “greatest” between two columns and display with some records already null in MySql
- Maximum size subset with given sum in C++
- How to find the greatest number in a list of numbers in Python?
- Find the greatest value among four tables in MySQL?

Advertisements