FastPrepFastPrep
Problem Brief

Longest Stable Transaction Period

OA

See the Image Source section at the very bottom of the page for the original problem statement πŸ»β€β„οΈπŸ»

In a bustling financial world, you are the guardian of stability, keeping a close eye on daily transactions. Each day’s net transaction value is recorded in an array, and your mission is to uncover the longest period of calm in this sea of numbers. But there’s a catch β€” a period is only considered stable if the sum of transactions over consecutive days can be evenly divided by a special number, the stability factor, k. Your goal is to write a function that searches through these records, finds the longest stretch of days where this condition holds true, and returns the length of that stable period.

1Example 1

Input
transactions = [2, 3, 1, 4, 1, 5, 9], k = 3
Output
4
Explanation

The longest stable period can be formed by the subarray [3, 1, 4, 1], where the sum 3 + 1 + 4 + 1 = 9 is divisible by 3.

2Example 2

Input
transactions = [3], k = 2
Output
0
Explanation

The array has only one element which is not divisible by 2, hence there are no stable periods and the result is 0.

Constraints

Limits and guarantees your solution can rely on.

πŸ“πŸ“
public int duolingoLongestTransaction(int[] transactions, int k) {
  // write your code here
}
Input

transactions

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

k

3

Output

4

Sign in to submit your solution.