Find Maximum Sum Strictly Increasing Subarray
Last Updated :
06 Jul, 2022
Given an array of positive integers. Find the maximum sum of strictly increasing subarrays. Note that this problem is different from maximum subarray sum and maximum sum increasing subsequence problems.
Examples:
Input : arr[] = {1, 2, 3, 2, 5, 1, 7}
Output : 8
Explanation : Some Strictly increasing subarrays are
{1, 2, 3} sum = 6,
{2, 5} sum = 7,
{1, 7} sum 8
Maximum Sum = 8
Input : arr[] = {1, 2, 2, 4}
Output: 6
Explanation : Increasing subarray with maximum sum is 6.
A Simple Solution is to generate all possible subarrays, and for every subarray check if subarray is strictly increasing or not. If subarray is strictly increasing, then we calculate sum & update max_sum. Time complexity O(n2).
An efficient solution of this problem take O(n) time. The idea is keep track of maximum sum and current sum. For every element arr[i], if it is greater than arr[i-1], then we add it to current sum. Else arr[i] is starting point of another potential candidate for maximum sum increasing subarray, so we update current sum as array. But before updating current sum, we update maximum sum if required.
Let input array be 'arr[]' and size of array be 'n'
Initialize :
max_sum = arr[0]
// because if array size is 1 than it
// would return that element.
// used to store the maximum sum
current_sum = arr[0] // used to compute current sum
// Traverse array starting from second element
i goes from 1 to n-1
// Check if it is strictly increasing then we
// update current_sum.
current_sum = current_sum + arr[i]
max_sum = max(max_sum, current_sum)
// Also needed for subarray having last element.
// else strictly increasing subarray breaks and
// arr[i] is starting point of next potential
// subarray
max_sum = max(max_sum, current_sum)
current_sum = arr[i]
return max(max_sum, current_sum)
Below is the implementation of the above idea.
C++
// C/C++ program to find the maximum sum of strictly
// increasing subarrays
#include <iostream>
using namespace std;
// Returns maximum sum of strictly increasing
// subarrays
int maxsum_SIS(int arr[], int n)
{
// Initialize max_sum be 0
int max_sum = arr[0];
// Initialize current sum be arr[0]
int current_sum = arr[0];
// Traverse array elements after first element.
for (int i = 1; i < n; i++)
{
// update current_sum for
// strictly increasing subarray
if (arr[i - 1] < arr[i])
{
current_sum = current_sum + arr[i];
max_sum = max(max_sum, current_sum);
}
else // strictly increasing subarray break
{
// update max_sum and current_sum ;
max_sum = max(max_sum, current_sum);
current_sum = arr[i];
}
}
return max(max_sum, current_sum);
}
// Driver code
int main()
{
int arr[] = { 1, 2, 2, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum sum : " << maxsum_SIS(arr, n);
return 0;
}
Java
// Java program to find the
// maximum sum of strictly increasing subarrays
public class GFG {
// Returns maximum sum
// of strictly increasing subarrays
static int maxsum_SIS(int arr[], int n)
{
// Initialize max_sum be 0
int max_sum = arr[0];
// Initialize current sum be arr[0]
int current_sum = arr[0];
// Traverse array elements after first element.
for (int i = 1; i < n; i++)
{
// update current_sum
// for strictly increasing subarray
if (arr[i - 1] < arr[i])
{
current_sum = current_sum + arr[i];
max_sum = Math.max(max_sum, current_sum);
}
else // strictly increasing subarray break
{
// update max_sum and current_sum ;
max_sum = Math.max(max_sum, current_sum);
current_sum = arr[i];
}
}
return Math.max(max_sum, current_sum);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 2, 4 };
int n = arr.length;
System.out.println("Maximum sum : "
+ maxsum_SIS(arr, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find the maximum sum of strictly
# increasing subarrays
# Returns maximum sum of strictly increasing
# subarrays
def maxsum_SIS(arr, n):
# Initialize max_sum be 0
max_sum = arr[0]
# Initialize current sum be arr[0]
current_sum = arr[0]
# Traverse array elements after first element.
for i in range(1, n):
# update current_sum for strictly increasing subarray
if (arr[i-1] < arr[i]):
current_sum = current_sum + arr[i]
max_sum = max(max_sum, current_sum)
else:
# strictly increasing subarray break
# update max_sum and current_sum
max_sum = max(max_sum, current_sum)
current_sum = arr[i]
return max(max_sum, current_sum)
# Driver code
def main():
arr = [1, 2, 2, 4]
n = len(arr)
print("Maximum sum : ", maxsum_SIS(arr, n)),
if __name__ == '__main__':
main()
# This code is contributed by 29AjayKumar
C#
// C# program to find the maximum sum of strictly
// increasing subarrays
using System;
public class GFG {
// Returns maximum sum of strictly increasing
// subarrays
static int maxsum_SIS(int[] arr, int n)
{
// Initialize max_sum be 0
int max_sum = arr[0];
// Initialize current sum be arr[0]
int current_sum = arr[0];
// Traverse array elements after first element.
for (int i = 1; i < n; i++) {
// update current_sum for strictly increasing
// subarray
if (arr[i - 1] < arr[i]) {
current_sum = current_sum + arr[i];
max_sum = Math.Max(max_sum, current_sum);
}
else // strictly increasing subarray break
{
// update max_sum and current_sum ;
max_sum = Math.Max(max_sum, current_sum);
current_sum = arr[i];
}
}
return Math.Max(max_sum, current_sum);
}
// Driver code
public static void Main()
{
int[] arr = { 1, 2, 2, 4 };
int n = arr.Length;
Console.WriteLine("Maximum sum : "
+ maxsum_SIS(arr, n));
}
}
// This code is contributed by 29AjayKumar
PHP
<?php
// PHP program to find the maximum sum of
// strictly increasing subarrays
// Returns maximum sum of strictly
// increasing subarrays
function maxsum_SIS($arr , $n)
{
// Initialize max_sum be 0
$max_sum = $arr[0];
// Initialize current sum be arr[0]
$current_sum = $arr[0];
// Traverse array elements after
// first element.
for ($i = 1; $i < $n ; $i++)
{
// update current_sum for strictly
// increasing subarray
if ($arr[$i - 1] < $arr[$i]){
$current_sum = $current_sum + $arr[$i];
$max_sum = max($max_sum, $current_sum);
}
else // strictly increasing
// subarray break
{
// update max_sum and current_sum ;
$max_sum = max($max_sum, $current_sum);
$current_sum = $arr[$i];
}
}
return max($max_sum, $current_sum);
}
// Driver Code
$arr = array(1, 2, 2, 4);
$n = sizeof($arr);
echo "Maximum sum : ",
maxsum_SIS($arr , $n);
// This code is contributed by Sachin
?>
JavaScript
<script>
// Javascript program to find the maximum sum of strictly
// increasing subarrays
// Returns maximum sum of strictly increasing
// subarrays
function maxsum_SIS(arr, n)
{
// Initialize max_sum be 0
var max_sum = arr[0];
// Initialize current sum be arr[0]
var current_sum = arr[0];
// Traverse array elements after first element.
for (var i = 1; i < n; i++)
{
// update current_sum for
// strictly increasing subarray
if (arr[i - 1] < arr[i])
{
current_sum = current_sum + arr[i];
max_sum = Math.max(max_sum, current_sum);
}
else // strictly increasing subarray break
{
// update max_sum and current_sum ;
max_sum = Math.max(max_sum, current_sum);
current_sum = arr[i];
}
}
return Math.max(max_sum, current_sum);
}
// Driver code
var arr = [ 1, 2, 2, 4 ];
var n = arr.length;
document.write( "Maximum sum : " + maxsum_SIS(arr, n));
// This code is contributed by itsok.
</script>
Time complexity : O(n)
Auxiliary Space : O(1)
Similar Reads
Count Strictly Increasing Subarrays Given an integer array arr[], the task is to count the number of subarrays in arr[] that are strictly increasing and have a size of at least 2. A subarray is a contiguous sequence of elements from arr[]. A subarray is strictly increasing if each element is greater than its previous element.Examples:
13 min read
Count number of strictly increasing and decreasing subarrays Given an array arr[] of size N, the task is to find the number of strictly increasing and decreasing subarrays.Examples: Input: arr[] = { 80, 50, 60, 70, 40, 40 } Output: 9 8Explanation: The increasing subarrays are: {80}, {50}, {60}, {70}, {40}, {40}, {50, 60}, {60, 70} and {50, 60, 70}. The decrea
9 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, 4
15 min read
Maximum sum bitonic subarray Given an array containing n numbers. The problem is to find the maximum sum bitonic subarray. A bitonic subarray is a subarray in which elements are first increasing and then decreasing. A strictly increasing or strictly decreasing subarray is also considered a bitonic subarray. Time Complexity of O
15+ min read
Largest sum contiguous increasing subarray Given an array of n positive distinct integers. The problem is to find the largest sum of contiguous increasing subarray in O(n) time complexity. Examples : Input : arr[] = {2, 1, 4, 7, 3, 6}Output : 12Contiguous Increasing subarray {1, 4, 7} = 12Input : arr[] = {38, 7, 8, 10, 12}Output : 38Recommen
15 min read