Count Transaction Streaks
At Flipkart, understanding customer purchasing patterns is crucial for optimizing product recommendations and targeted promotions. One such pattern involves identifying 'transaction streaks' — consecutive sequences of purchases whose total value matches a specific target. This problem asks you to analyze a list of individual transaction values to find how many such streaks exist.
Given an array transactions representing the monetary values of a series of individual customer transactions, and a targetSum, your task is to implement a function that returns the total number of contiguous subarrays (transaction streaks) within transactions whose elements sum up to exactly targetSum.
transactions: An array of numbers (number[]), where each element transactions[i] represents the value of an individual transaction.targetSum: A number (number), representing the desired sum for a transaction streak.number), which is the total count of contiguous subarrays in transactions that sum up to targetSum.transactions array: If the transactions array is empty, no subarrays can be formed, so the function should return 0.targetSum can be zero: The target sum itself can be zero. This should also be handled correctly, especially in arrays containing zero values.Examples
Example 1
Input:
transactions = [1, 2, 3, -1, 4], targetSum = 5
Output:
2
Note:
The subarrays that sum to 5 are: [2, 3] (elements at index 1 and 2) and [1, 2, 3, -1] (elements at index 0 to 3).
Example 2
Input:
transactions = [1, 1, 1], targetSum = 2
Output:
2
Note:
The subarrays that sum to 2 are: [1, 1] (first two elements) and [1, 1] (last two elements).
Example 3
Input:
transactions = [0, 0, 0], targetSum = 0
Output:
6
Note:
All possible contiguous subarrays sum to 0: [0] (3 times), [0, 0] (2 times), and [0, 0, 0] (1 time). Total 3 + 2 + 1 = 6.
Constraints
1 <= transactions.length <= 2 * 10^4-1000 <= transactions[i] <= 1000-10^7 <= targetSum <= 10^7transactions.targetSum?targetSum being zero?Loading editor...