Given an array arr[] of size N, the task is to count the number of subsets of arr[] whose mean is maximum.
Examples:
Input: arr[] = {1, 2, 1, 2}
Output: 3
Subsets with maximum mean are {2}, {2} and {2, 2}.Input: arr[] = {1}
Output: 1
Approach: The maximum value for the mean of any subset will be when the subset will only consist of the maximum element from the array. So, in order to count all the possible subsets, find the frequency of the maximum element from the array say cnt and the count of possible subsets will be 2cnt – 1.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the coutn of // subsets with the maximum mean int cntSubSets(int arr[], int n) { // Maximum value from the array int maxVal = *max_element(arr, arr + n); // To store the number of times maximum // element appears in the array int cnt = 0; for (int i = 0; i < n; i++) { if (arr[i] == maxVal) cnt++; } // Return the count of valid subsets return (pow(2, cnt) - 1); } // Driver code int main() { int arr[] = { 1, 2, 1, 2 }; int n = sizeof(arr) / sizeof(int); cout << cntSubSets(arr, n); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the coutn of // subsets with the maximum mean static int cntSubSets(int arr[], int n) { // Maximum value from the array int maxVal = Arrays.stream(arr).max().getAsInt(); // To store the number of times maximum // element appears in the array int cnt = 0; for (int i = 0; i < n; i++) { if (arr[i] == maxVal) cnt++; } // Return the count of valid subsets return (int) (Math.pow(2, cnt) - 1); } // Driver code public static void main(String []args) { int arr[] = { 1, 2, 1, 2 }; int n = arr.length; System.out.println(cntSubSets(arr, n)); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach # Function to return the coutn of # subsets with the maximum mean def cntSubSets(arr, n) : # Maximum value from the array maxVal = max(arr); # To store the number of times maximum # element appears in the array cnt = 0; for i in range(n) : if (arr[i] == maxVal) : cnt += 1; # Return the count of valid subsets return ((2 ** cnt) - 1); # Driver code if __name__ == "__main__" : arr= [ 1, 2, 1, 2 ]; n = len(arr); print(cntSubSets(arr, n)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; using System.Linq; class GFG { // Function to return the coutn of // subsets with the maximum mean static int cntSubSets(int []arr, int n) { // Maximum value from the array int maxVal = arr.Max(); // To store the number of times maximum // element appears in the array int cnt = 0; for (int i = 0; i < n; i++) { if (arr[i] == maxVal) cnt++; } // Return the count of valid subsets return (int) (Math.Pow(2, cnt) - 1); } // Driver code public static void Main(String []args) { int []arr = { 1, 2, 1, 2 }; int n = arr.Length; Console.WriteLine(cntSubSets(arr, n)); } } // This code is contributed by 29AjayKumar |
3
Time Complexity: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find Harmonic mean using Arithmetic mean and Geometric mean
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Sum of elements in an array whose difference with the mean of another array is less than k
- Count number of subsets whose median is also present in the same subset
- Product of all sorted subsets of size K using elements whose index divide K completely
- Choose n elements such that their mean is maximum
- Length of longest sub-array with maximum arithmetic mean.
- Split array into minimum number of subsets having maximum pair sum at most K
- Number of ways to remove elements to maximize arithmetic mean
- Removing a number from array without changing its arithmetic mean
- Maximum and Minimum Product Subsets
- Sum of maximum elements of all subsets
- k size subsets with maximum difference d between max and min
- Maximum difference between two subsets of m elements
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
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.

