Number of subsets whose mean is maximum

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 count of// subsets with the maximum meanint 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 codeint main(){ int arr[] = { 1, 2, 1, 2 }; int n = sizeof(arr) / sizeof(int); cout << cntSubSets(arr, n); return 0;} |
Java
// Java implementation of the approachimport java.util.*;class GFG {// Function to return the count of// subsets with the maximum meanstatic 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 codepublic 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 count 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 approachusing System;using System.Linq; class GFG {// Function to return the count of// subsets with the maximum meanstatic 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 codepublic 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 |
Javascript
<script>// Javascript implementation of the approach// Function to return the count of// subsets with the maximum meanfunction cntSubSets(arr, n){ // Maximum value from the array var maxVal = arr.reduce(function(a, b) { return Math.max(a, b); }); // To store the number of times maximum // element appears in the array var cnt = 0; for (var i = 0; i < n; i++) { if (arr[i] == maxVal) cnt++; } // Return the count of valid subsets return (Math.pow(2, cnt) - 1);}// Driver codevar arr = [ 1, 2, 1, 2 ]var n = arr.length;document.write(cntSubSets(arr, n));</script> |
Output:
3
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



