# Sum of minimum element of all sub-sequences of a sorted array

Given a sorted array A of n integers. The task is to find the sum of minimum of all possible subsequences of A.

Note: Considering there will be no overflow of numbers.

Examples:

Input: A = [ 1, 2, 4, 5]
Output: 17
Subsequences are , , , , [1, 2], [1, 4], [1, 5], [2, 4], [2, 5], [4, 5] [1, 2, 4], [1, 2, 5], [1, 4, 5], [2, 4, 5], [1, 2, 4, 5]
Minimums are 1, 2, 4, 5, 1, 1, 1, 2, 2, 4, 1, 1, 1, 2, 1.
Sum is 29

Input: A = [1, 2, 3]
Output: 11

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The Naive approach is to generate all possible subsequences, find their minimum and add them to result.

Efficient Approach: It is given that the array is sorted, so observe that minimum element occurs 2n-1 times, the second minimum occurs 2n-2 times and so on… Let’s take an example:

arr[] = {1, 2, 3}
Subsequences are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}
Minimum of each subsequence: {1}, {2}, {3}, {1}, {1}, {2}, {1}.
where
1 occurs 4 times i.e. 2 n-1 where n = 3.
2 occurs 2 times i.e. 2n-2 where n = 3.
3 occurs 1 times i.e. 2n-3 where n = 3.

So, traverse the array and add current element i.e. arr[i]* pow(2, n-1-i) to the sum.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the sum ` `// of minimum of all subsequence ` `int` `findMinSum(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``int` `occ = n - 1, sum = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``sum += arr[i] * ``pow``(2, occ); ` `        ``occ--; ` `    ``} ` ` `  `    ``return` `sum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 2, 4, 5 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << findMinSum(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the above approach  ` `class` `GfG  ` `{ ` ` `  `// Function to find the sum  ` `// of minimum of all subsequence  ` `static` `int` `findMinSum(``int` `arr[], ``int` `n)  ` `{  ` ` `  `    ``int` `occ = n - ``1``, sum = ``0``;  ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{  ` `        ``sum += arr[i] * (``int``)Math.pow(``2``, occ);  ` `        ``occ--;  ` `    ``}  ` ` `  `    ``return` `sum;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `arr[] = { ``1``, ``2``, ``4``, ``5` `};  ` `    ``int` `n = arr.length;  ` ` `  `    ``System.out.println(findMinSum(arr, n));  ` `} ` `}  ` ` `  `// This code is contributed by Prerna Saini `

## Python3

 `# Python3 implementation of the  ` `# above approach ` ` `  `# Function to find the sum ` `# of minimum of all subsequence ` `def` `findMinSum(arr, n): ` ` `  `    ``occ ``=` `n ``-` `1` `    ``Sum` `=` `0` `    ``for` `i ``in` `range``(n): ` `        ``Sum` `+``=` `arr[i] ``*` `pow``(``2``, occ) ` `        ``occ ``-``=` `1` `     `  `    ``return` `Sum` ` `  `# Driver code ` `arr ``=` `[``1``, ``2``, ``4``, ``5``] ` `n ``=` `len``(arr) ` ` `  `print``(findMinSum(arr, n)) ` ` `  `# This code is contributed  ` `# by mohit kumar `

## C#

 `// C# implementation of the above approach  ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to find the sum  ` `// of minimum of all subsequence  ` `static` `int` `findMinSum(``int` `[]arr, ``int` `n)  ` `{  ` ` `  `    ``int` `occ = n - 1, sum = 0;  ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{  ` `        ``sum += arr[i] *(``int``) Math.Pow(2, occ);  ` `        ``occ--;  ` `    ``}  ` ` `  `    ``return` `sum;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main(String []args)  ` `{  ` `    ``int` `[]arr = { 1, 2, 4, 5 };  ` `    ``int` `n = arr.Length;  ` ` `  `    ``Console.WriteLine( findMinSum(arr, n));  ` `} ` `}  ` `// This code is contributed by Arnab Kundu `

## PHP

 ` `

Output:

```29
```

Note: To find the Sum of maximum element of all subsequences in a sorted array, just traverse the array in reverse order and apply the same formula for Sum.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.