# Find Maximum Sum Strictly Increasing Subarray

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.
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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 = 0 // 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]

// 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 implementation of above idea.

## C++

 `// C/C++ program to find the maximum sum of strictly ` `// increasing subarrays ` `#include ` `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 = 0; ` ` `  `    ``// Initialize current sum be arr[0] ` `    ``int` `current_sum = arr[0] ; ` ` `  `    ``// Traverse array elements after first element. ` `    ``for` `(``int` `i=1; i

## 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 = ``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]; ` `            ``} ``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 program ` `    ``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 ``=` `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] ` `  `  `  `  `        ``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 program ` `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 = 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];  ` `            ``} ``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 program  ` `    ``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

 ` `

Output:

```Maximum sum : 6
```

Time complexity : O(n)
Auxiliary Space : O(1)

This article is contributed by Nishant_Singh(Pintu). 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.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Improved By : 29AjayKumar, Sach_Code

Article Tags :
Practice Tags :

1

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