Problem · Array
Wallet Balance on a Selected Day
Learn this problemProblem statement
A wallet starts with balance 0. Its daily transactions are stored in an integer array transactions: a positive value is a credit, a negative value is a debit, and zero makes no change.
Given a zero-based index selectedDay, return the wallet balance at the end of that day. Include every transaction from day 0 through selectedDay. Return a 64-bit integer.
Function
balanceOnDay(transactions: int[], selectedDay: int) → longExamples
Example 1
transactions = [100,-30,20,-10]selectedDay = 2return = 90After days 0 through 2, the balance is 100 - 30 + 20 = 90.
Example 2
transactions = [-50,80,-20]selectedDay = 0return = -50Only the first day's debit is included.
Example 3
transactions = [5,0,-2,7]selectedDay = 3return = 10The full history sums to 5 + 0 - 2 + 7 = 10.
Constraints
1 <= transactions.length <= 5000-10^6 <= transactions[i] <= 10^60 <= selectedDay < transactions.length