Sum of all subsets of a given size

Given an array arr[] consisting of N integers and a positive integer K, the task is to find the sum of all the subsets of size K.Examples:Input: arr[] = {1, 2, 4, 5}, K = 2Output: 36Explanation:The subsets of size K(= 2) are = {1, 2}, {1, 4}, {1, 5}, {2, 4}, {2, 5}, {4, 5}. Now, the sumof all subsets sum = 3 + 5 + 6 + 6 + 7 + 9 = 36.Input: arr[] = {2, 4, 5, 6, 8}Output: 150Naive Approach: The simplest approach to solve the given problem is to generate all possible subsets of the given array and find the sum of elements of those subsets whose size is K. After finding the sum of all K sized subsets, print the sum of all the sums obtained as the result.Time Complexity: O(K*2N)Auxiliary Space: O(1)Efficient Approach: The above approach can also be optimized by observing the fact that the number of occurrences of each element arr[i] in the summation series is dependent on its position in array arr[] i.e., i which is equal to 2(N – 1). Consider the fact that:Occurence of x in size – 1 subsets = n-1C1-1 = n-1C0Occurence of x in size – 2 subsets = n-1C2-1 = n-1C1, so onOccurence of x in size – n subsets = n-1Cn-1 Therefore, the total frequency of any element X in the summation equation = n-1C0 + n-1C1 + n-1C2 + … + n-1Cn-1 = 2n-1. Hence, the sum of all the subsets = (Sum of all elements of the array) * 2n – 1.Follow the steps below to solve the given problem:Below is the implementation of the above approach:C++#include using namespace std;  void findSumOfAllSubsets(int arr[], int n, int K){              int freq = pow(2, K) – 1;          int sum = 0;    for (int i = 0; i < n; i++)        sum += arr[i];          sum = sum * freq;      cout