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 the minimum of all possible subsequences of A.
Note: Considering there will be no overflow of numbers.
Examples:Â
Input: A = [1, 2, 4, 5]Â
Output: 29Â
Subsequences are [1], [2], [4], [5], [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Â Â
Approach: The Naive approach is to generate all possible subsequences, find their minimum and add them to the result.Â
Efficient Approach: It is given that the array is sorted, so observe that the 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 <bits/stdc++.h>using namespace std;Â
// Function to find the sum// of minimum of all subsequenceint 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 codeint main(){Â Â Â Â int arr[] = { 1, 2, 4, 5 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â
    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 subsequencedef findMinSum(arr, n):Â
    occ = n - 1    Sum = 0    for i in range(n):        Sum += arr[i] * pow(2, occ)        occ -= 1         return SumÂ
# Driver codearr = [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
<?php// PHP implementation of the// above approachÂ
// Function to find the sum// of minimum of all subsequencefunction findMinSum($arr, $n){Â Â Â Â $occ1 = ($n);Â Â Â Â $occ = $occ1 - 1;Â Â Â Â $Sum = 0;Â Â Â Â for ($i = 0; $i < $n; $i++) Â Â Â Â {Â Â Â Â Â Â Â Â $Sum += $arr[$i] * pow(2, $occ);Â Â Â Â Â Â Â Â $occ -= 1;Â Â Â Â }Â Â Â Â return $Sum;}Â
// Driver code$arr = array(1, 2, 4, 5);$n = count($arr);Â
echo findMinSum($arr, $n);Â
// This code is contributed// by Srathore?> |
Javascript
<script>Â
// Javascript implementation of the above approachÂ
// Function to find the sum// of minimum of all subsequencefunction findMinSum(arr, n){Â
    var occ = n - 1, sum = 0;    for (var i = 0; i < n; i++) {        sum += arr[i] * Math.pow(2, occ);        occ--;    }Â
    return sum;}Â
// Driver codevar arr = [ 1, 2, 4, 5 ];var n = arr.length;document.write( findMinSum(arr, n));Â
</script> |
29
Â
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
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.
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



