BackmediumArraysPayPalAdobe

Alternating Budget Accumulation Solution

Problem Statement

Given an array of integers budget where positive values represent excess funds and negative values represent shortfalls, determine the maximum possible sum of alternating budget intervals where the first interval has excess funds, the second has a shortfall, and this pattern continues.

Example 1
Input
[1, -2, 3, -4, 5]
Output
5

Explanation: Step-by-step: with input [1, -2, 3, -4, 5], we first initialize max_sum as -inf. Then we iterate through the array, and for each element, we check if it's positive (excess funds). If it is, we update max_sum as max(max_sum, current_sum). If it's negative (shortfall), we update max_sum as max(max_sum, current_sum - current_element). Finally, we return max_sum, which is 5.

Example 2
Input
[-1, 2, -3, 4, -5]
Output
4

Explanation: Step-by-step: with input [-1, 2, -3, 4, -5], we first initialize max_sum as -inf. Then we iterate through the array, and for each element, we check if it's positive (excess funds). If it is, we update max_sum as max(max_sum, current_sum). If it's negative (shortfall), we update max_sum as max(max_sum, current_sum - current_element). Finally, we return max_sum, which is 4.

Constraints

  • 1 <= array length <= 1000
  • -1000 <= array element <= 1000
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Alternating Budget Accumulation — Problem Statement & Solution Guide

ArraysMediumprefix sum modification
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of integers budget where positive values represent excess funds and negative values represent shortfalls, determine the maximum possible sum of alternating budget intervals where the first interval has excess funds, the second has a shortfall, and this pattern continues.

Examples

Example 1

Input

[1, -2, 3, -4, 5]

Output

5

Explanation: Step-by-step: with input [1, -2, 3, -4, 5], we first initialize max_sum as -inf. Then we iterate through the array, and for each element, we check if it's positive (excess funds). If it is, we update max_sum as max(max_sum, current_sum). If it's negative (shortfall), we update max_sum as max(max_sum, current_sum - current_element). Finally, we return max_sum, which is 5.

Example 2

Input

[-1, 2, -3, 4, -5]

Output

4

Explanation: Step-by-step: with input [-1, 2, -3, 4, -5], we first initialize max_sum as -inf. Then we iterate through the array, and for each element, we check if it's positive (excess funds). If it is, we update max_sum as max(max_sum, current_sum). If it's negative (shortfall), we update max_sum as max(max_sum, current_sum - current_element). Finally, we return max_sum, which is 4.

Constraints

  • 1 <= array length <= 1000
  • -1000 <= array element <= 1000

Verified Code Solutions

No solution currently available for JavaScript. Select another language tab above.

Asked in Top Tech Interviews

PayPalAdobe

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.