BackmediumArrays

Fluctuating Treasury Balance Solution

Problem Statement

A financial analyst is tracking a company's daily treasury fluctuations. You are given an integer array arr representing daily transaction values. The analyst wants to evaluate the net performance of several campaigns. Each campaign is defined by a range [L, R] (0-indexed). The net performance of a campaign is calculated by alternatingly adding and subtracting the daily transaction values starting from the first day of the campaign.

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

Explanation: Step-by-step: with input [1, -2, 3, -4, 5], we alternate between adding and subtracting the daily transaction values. Starting with 0, we add 1, subtract 2, add 3, subtract 4, and add 5. The final result is 3.

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

Explanation: Step-by-step: with input [-1, 2, -3, 4, -5], we alternate between adding and subtracting the daily transaction values. Starting with 0, we subtract 1, add 2, subtract 3, add 4, and subtract 5. The final result is -3.

Constraints

  • 1 <= arr.length <= 10^5
  • 1 <= queries.length <= 10^5
  • queries[i].length == 2
  • 0 <= queries[i][0] <= queries[i][1] < arr.length
  • -10^4 <= arr[i] <= 10^4
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

Fluctuating Treasury Balance — Problem Statement & Solution Guide

ArraysMediumRange Sum Query using Prefix Sum
TimeO(n + q)
|
SpaceO(n)

Problem Description

A financial analyst is tracking a company's daily treasury fluctuations. You are given an integer array arr representing daily transaction values. The analyst wants to evaluate the net performance of several campaigns. Each campaign is defined by a range [L, R] (0-indexed). The net performance of a campaign is calculated by alternatingly adding and subtracting the daily transaction values starting from the first day of the campaign.

Examples

Example 1

Input

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

Output

3

Explanation: Step-by-step: with input [1, -2, 3, -4, 5], we alternate between adding and subtracting the daily transaction values. Starting with 0, we add 1, subtract 2, add 3, subtract 4, and add 5. The final result is 3.

Example 2

Input

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

Output

-3

Explanation: Step-by-step: with input [-1, 2, -3, 4, -5], we alternate between adding and subtracting the daily transaction values. Starting with 0, we subtract 1, add 2, subtract 3, add 4, and subtract 5. The final result is -3.

Constraints

  • 1 <= arr.length <= 10^5
  • 1 <= queries.length <= 10^5
  • queries[i].length == 2
  • 0 <= queries[i][0] <= queries[i][1] < arr.length
  • -10^4 <= arr[i] <= 10^4

Optimal Approach & Strategy

We precompute an alternating prefix sum array where each element at index i adds arr[i] if i is even, and subtracts arr[i] if i is odd. For each query [L, R], we compute the difference between prefix[R+1] and prefix[L]. If L is odd, the query starts with a negative term relative to our absolute prefix sum, so we simply negate the difference. This allows us to answer each query in O(1) time.

Brute Force Approach

Iterate through each query range [L, R] and manually simulate the alternating addition and subtraction element by element. This requires a nested loop where the outer loop iterates over all queries and the inner loop travels from index L to R, resulting in an O(Q * N) time complexity which will time out for large arrays.

Verified Code Solutions

JavaScript Solution
Time: O(n + q)
function solution(nums) {
   let result = 0;
   let add = true;
   for (let num of nums) {
       if (add) {
           result += num;
       } else {
           result -= num;
       }
       add = !add;
   }
   return result;
}

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.