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
  • Natural Numbers
  • Whole Numbers
  • Real Numbers
  • Integers
  • Rational Numbers
  • Irrational Numbers
  • Complex Numbers
  • Prime Numbers
  • Odd Numbers
  • Even Numbers
  • Properties of Numbers
  • Number System
Open In App
Next Article:
Subsequence with maximum odd sum
Next article icon

Maximum Sum Subsequence

Last Updated : 25 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] of size N, the task is to find the maximum sum non-empty subsequence present in the given array.

Examples:

Input: arr[] = { 2, 3, 7, 1, 9 } 
Output: 22 
Explanation: 
Sum of the subsequence { arr[0], arr[1], arr[2], arr[3], arr[4] } is equal to 22, which is the maximum possible sum of any subsequence of the array. 
Therefore, the required output is 22.

Input: arr[] = { -2, 11, -4, 2, -3, -10 } 
Output: 13 
Explanation: 
Sum of the subsequence { arr[1], arr[3] } is equal to 13, which is the maximum possible sum of any subsequence of the array. 
Therefore, the required output is 13.

Naive Approach: The simplest approach to solve this problem is to generate all possible non-empty subsequences of the array and calculate the sum of each subsequence of the array. Finally, print the maximum sum obtained from the subsequence.

 Time Complexity: O(N * 2N) 
Auxiliary Space: O(N)

Efficient Approach: The idea is to traverse the array and calculate the sum of positive elements of the array and print the sum obtained. Follow the steps below to solve the problem:

  • Check if the largest element of the array is greater than 0 or not. If found to be true, then traverse the array and print the sum of all positive elements of the array.
  • Otherwise, print the largest element present in the array.

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the maximum
// non-empty subsequence sum
int MaxNonEmpSubSeq(int a[], int n)
{
    // Stores the maximum non-empty
    // subsequence sum in an array
    int sum = 0;
 
    // Stores the largest element
    // in the array
    int max = *max_element(a, a + n);
 
    if (max <= 0) {
 
        return max;
    }
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If a[i] is greater than 0
        if (a[i] > 0) {
 
            // Update sum
            sum += a[i];
        }
    }
    return sum;
}
 
// Driver Code
int main()
{
    int arr[] = { -2, 11, -4, 2, -3, -10 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << MaxNonEmpSubSeq(arr, N);
 
    return 0;
}
 
 

Java




// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
  // Function to print the maximum
  // non-empty subsequence sum
  static int MaxNonEmpSubSeq(int a[], int n)
  {
 
    // Stores the maximum non-empty
    // subsequence sum in an array
    int sum = 0;
 
    // Stores the largest element
    // in the array
    int max = a[0];
    for(int i = 1; i < n; i++)
    {
      if(max < a[i])
      {
        max = a[i];
      }
    }
 
    if (max <= 0)
    {    
      return max;
    }
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
      // If a[i] is greater than 0
      if (a[i] > 0)
      {
 
        // Update sum
        sum += a[i];
      }
    }
    return sum;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int arr[] = { -2, 11, -4, 2, -3, -10 };
    int N = arr.length;
 
    System.out.println(MaxNonEmpSubSeq(arr, N));
  }
}
 
// This code is contributed by divyesh072019
 
 

Python3




# Python3 program to implement
# the above approach
 
# Function to print the maximum
# non-empty subsequence sum
def MaxNonEmpSubSeq(a, n):
     
    # Stores the maximum non-empty
    # subsequence sum in an array
    sum = 0
 
    # Stores the largest element
    # in the array
    maxm = max(a)
 
    if (maxm <= 0):
        return maxm
 
    # Traverse the array
    for i in range(n):
         
        # If a[i] is greater than 0
        if (a[i] > 0):
             
            # Update sum
            sum += a[i]
             
    return sum
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ -2, 11, -4, 2, -3, -10 ]
    N = len(arr)
 
    print(MaxNonEmpSubSeq(arr, N))
 
# This code is contributed by mohit kumar 29
 
 

C#




// C# program to implement
// the above approach
using System;
 
class GFG{
     
// Function to print the maximum
// non-empty subsequence sum
static int MaxNonEmpSubSeq(int[] a, int n)
{
     
    // Stores the maximum non-empty
    // subsequence sum in an array
    int sum = 0;
     
    // Stores the largest element
    // in the array
    int max = a[0];
    for(int i = 1; i < n; i++)
    {
        if (max < a[i])
        {
            max = a[i];
        }
    }
     
    if (max <= 0)
    {
        return max;
    }
     
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // If a[i] is greater than 0
        if (a[i] > 0)
        {
             
            // Update sum
            sum += a[i];
        }
    }
    return sum;
}
 
// Driver Code
static void Main()
{
    int[] arr = { -2, 11, -4, 2, -3, -10 };
    int N = arr.Length;
     
    Console.WriteLine(MaxNonEmpSubSeq(arr, N));
}
}
 
// This code is contributed by divyeshrabadiya07
 
 

Javascript




<script>
 
    // Javascript program to implement
    // the above approach
     
    // Function to print the maximum
    // non-empty subsequence sum
    function MaxNonEmpSubSeq(a, n)
    {
 
        // Stores the maximum non-empty
        // subsequence sum in an array
        let sum = 0;
 
        // Stores the largest element
        // in the array
        let max = a[0];
        for(let i = 1; i < n; i++)
        {
            if (max < a[i])
            {
                max = a[i];
            }
        }
 
        if (max <= 0)
        {
            return max;
        }
 
        // Traverse the array
        for(let i = 0; i < n; i++)
        {
 
            // If a[i] is greater than 0
            if (a[i] > 0)
            {
 
                // Update sum
                sum += a[i];
            }
        }
        return sum;
    }
     
    let arr = [ -2, 11, -4, 2, -3, -10 ];
    let N = arr.length;
      
    document.write(MaxNonEmpSubSeq(arr, N));
   
</script>
 
 
Output: 
13

 

Time Complexity: O(N) 
Auxiliary Space: O(1)

 



Next Article
Subsequence with maximum odd sum

C

CoderSaty
Improve
Article Tags :
  • Arrays
  • DSA
  • Greedy
  • Mathematical
  • Searching
  • Numbers
  • subsequence
Practice Tags :
  • Arrays
  • Greedy
  • Mathematical
  • Numbers
  • Searching

Similar Reads

  • Maximum even sum subsequence
    Given a array of n positive and negative integers, find the subsequence with the maximum even sum and display that even sum. Examples: Input: arr[] = {-2, 2, -3, 1, 3} Output: 6 Explanation: The longest subsequence with even sum is 2, 1 and 3. Input: arr[] = {-2, 2, -3, 4, 5} Output: 8 Explanation:
    6 min read
  • Subsequence with maximum odd sum
    Given a set of integers, check whether there is a subsequence with odd sum and if yes, then finding the maximum odd sum. If no subsequence contains odd sum, return -1. Examples : Input : arr[] = {2, 5, -4, 3, -1}; Output : 9 The subsequence with maximum odd sum is 2, 5, 3 and -1. Input : arr[] = {4,
    9 min read
  • Maximum Sum Increasing Subsequence
    Given an array arr[] of n positive integers. The task is to find the sum of the maximum sum subsequence of the given array such that the integers in the subsequence are sorted in strictly increasing order. Examples: Input: arr[] = [1, 101, 2, 3, 100]Output: 106Explanation: The maximum sum of a incre
    15+ min read
  • Maximum Sum Decreasing Subsequence
    Given an array of N positive integers. The task is to find the sum of the maximum sum decreasing subsequence(MSDS) of the given array such that the integers in the subsequence are sorted in decreasing order. Examples: Input: arr[] = {5, 4, 100, 3, 2, 101, 1} Output: 106 100 + 3 + 2 + 1 = 106 Input:
    7 min read
  • Maximum Sum Subsequence of length k
    Given an array sequence [A1, A2 ...An], the task is to find the maximum possible sum of increasing subsequence S of length k such that S1<=S2<=S3.........<=Sk. Examples: Input : n = 8 k = 3 A=[8 5 9 10 5 6 21 8] Output : 40 Possible Increasing subsequence of Length 3 with maximum possible s
    11 min read
  • Maximum sum alternating subsequence
    Given an array, the task is to find sum of maximum sum alternating subsequence starting with first element. Here alternating sequence means first decreasing, then increasing, then decreasing, ... For example 10, 5, 14, 3 is an alternating sequence. Note that the reverse type of sequence (increasing
    13 min read
  • Longest subsequence having maximum sum
    Given an array arr[] of size N, the task is to find the longest non-empty subsequence from the given array whose sum is maximum. Examples: Input: arr[] = { 1, 2, -4, -2, 3, 0 } Output: 1 2 3 0 Explanation: Sum of elements of the subsequence {1, 2, 3, 0} is 6 which is the maximum possible sum. Theref
    8 min read
  • Maximum sum subsequence of length K | Set 2
    Given an array sequence arr[] i.e [A1, A2 …An] and an integer k, the task is to find the maximum possible sum of increasing subsequence S of length k such that S1<=S2<=S3………<=Sk. Examples: Input: arr[] = {-1, 3, 4, 2, 5}, K = 3Output: 3 4 5Explanation: Subsequence 3 4 5 with sum 12 is the s
    7 min read
  • Maximum sum Bi-tonic Sub-sequence
    Given an array of integers. A subsequence of arr[] is called Bitonic if it is first increasing, then decreasing. Examples : Input : arr[] = {1, 15, 51, 45, 33, 100, 12, 18, 9} Output : 194 Explanation : Bi-tonic Sub-sequence are : {1, 51, 9} or {1, 51, 100, 18, 9} or {1, 15, 51, 100, 18, 9} or {1, 1
    10 min read
  • Printing Maximum Sum Increasing Subsequence
    The Maximum Sum Increasing Subsequence problem is to find the maximum sum subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. Examples: Input: [1, 101, 2, 3, 100, 4, 5]Output: [1, 2, 3, 100]Input: [3, 4, 5, 10]Output: [3, 4, 5, 10]Input: [10, 5,
    15 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