Sum of average of all subsets - GeeksforGeeks
Given an array arr of N integer elements, the task is to find sum of average of all subsets of this array.
Input : arr[] = [2, 3, 5] Output : 23.33 Explanation : Subsets with their average are, [2] average = 2/1 = 2 [3] average = 3/1 = 3 [5] average = 5/1 = 5 [2, 3] average = (2+3)/2 = 2.5 [2, 5] average = (2+5)/2 = 3.5 [3, 5] average = (3+5)/2 = 4 [2, 3, 5] average = (2+3+5)/3 = 3.33 Sum of average of all subset is, 2 + 3 + 5 + 2.5 + 3.5 + 4 + 3.33 = 23.33
We strongly recommend you to minimize your browser and try this yourself first.
A naive solution is to iterate through all possible subsets, get average of all of them and then add them one by one, but this will take exponential time and will be infeasible for bigger arrays.
Read full article from Sum of average of all subsets - GeeksforGeeks
No comments:
Post a Comment