Sum of values of all possible non | C ++ implementation of the approach ; Function to return the required sum ; Find the sum of the array elements ; Every element appears 2 ^ ( n - 1 ) times ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } sum = sum * pow ( 2 , n - 1 ) ; return sum ; } int main ( ) { int arr [ ] = { 2 , 1 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << sum ( arr , n ) ; return 0 ; }