Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Sum of minimum elements of all subarrays
Next article icon

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

Last Updated : 22 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 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[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 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




<?php
// PHP implementation of the
// above approach
 
// Function to find the sum
// of minimum of all subsequence
function 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 subsequence
function 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 code
var arr = [ 1, 2, 4, 5 ];
var n = arr.length;
document.write( findMinSum(arr, n));
 
</script>
 
 
Output: 
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.
 



Next Article
Sum of minimum elements of all subarrays

S

Shivam.Pradhan
Improve
Article Tags :
  • Arrays
  • DSA
  • Mathematical
  • maths-power
  • subsequence
Practice Tags :
  • Arrays
  • Mathematical

Similar Reads

  • Sum of minimum element of all subarrays 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 subarrays of A. Examples: Input: A = [ 1, 2, 4, 5] Output: 23 Subsequences are [1], [2], [4], [5], [1, 2], [2, 4], [4, 5] [1, 2, 4], [2, 4, 5], [1, 2, 4, 5] Minimums are 1, 2, 4, 5, 1, 2, 4, 1, 2, 1. Sum
    4 min read
  • Minimum sum of medians of all possible K length subsequences of a sorted array
    Given a sorted array arr[] consisting of N integers and a positive integer K(such that N%K is 0), the task is to find the minimum sum of the medians of all possible subsequences of size K such that each element belongs to only one subsequence. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 2Output
    7 min read
  • Sum of minimum elements of all subarrays
    Given an array A of n integers. The task is to find the sum of minimum of all possible (contiguous) subarray of A. Examples: Input: A = [3, 1, 2, 4] Output: 17 Explanation: Subarrays are [3], [1], [2], [4], [3, 1], [1, 2], [2, 4], [3, 1, 2], [1, 2, 4], [3, 1, 2, 4]. Minimums are 3, 1, 2, 4, 1, 1, 2,
    15+ min read
  • Minimum sum possible by removing all occurrences of any array element
    Given an array arr[] consisting of N integers, the task is to find the minimum possible sum of the array by removing all occurrences of any single array element. Examples: Input: N = 4, arr[] = {4, 5, 6, 6}Output: 9Explanation: All distinct array elements are {4, 5, 6}. Removing all occurrences of 4
    6 min read
  • Sum of all minimum occurring elements in an Array
    Given an array of integers containing duplicate elements. The task is to find the sum of all least occurring elements in the given array. That is the sum of all such elements whose frequency is minimum in the array.Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3, 3} Output : 2 The least occurring ele
    6 min read
  • Find sum of sum of all sub-sequences
    Given an array of n integers. The task is to find the sum of each sub-sequence of the array. Examples : Input : arr[] = { 6, 8, 5 } Output : 76 All subsequence sum are: { 6 }, sum = 6 { 8 }, sum = 8 { 5 }, sum = 5 { 6, 8 }, sum = 14 { 6, 5 }, sum = 11 { 8, 5 }, sum = 13 { 6, 8, 5 }, sum = 19 Total s
    4 min read
  • Bitwise OR of sum of all subsequences of an array
    Given an array arr[] of length N, the task is to find the Bitwise OR of the sum of all possible subsequences from the given array. Examples: Input: arr[] = {4, 2, 5}Output: 15Explanation: All subsequences from the given array and their corresponding sums:{4} - 4{2} - 2{5} - 5{4, 2} - 6{4, 5} - 9{2,
    6 min read
  • Minimum number of deletions to make a sorted sequence
    Given an array of n integers. The task is to remove or delete the minimum number of elements from the array so that when the remaining elements are placed in the same sequence order to form an increasing sorted sequence. Examples : Input : {5, 6, 1, 7, 4}Output : 2Removing 1 and 4leaves the remainin
    15+ min read
  • Minimize the sum of MEX by removing all elements of array
    Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
    7 min read
  • Minimize sum of distinct elements of all prefixes by rearranging Array
    Given an array arr[] with size N, the task is to find the minimum possible sum of distinct elements over all the prefixes of the array that can be obtained by rearranging the elements of the array. Examples: Input: arr[] = {3, 3, 2, 2, 3}, N = 5Output: 7Explanation: The permutation arr[] = {3, 3, 3,
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences